【问题标题】:Calling an Ember _super method from Promise handler从 Promise 处理程序调用 Ember _super 方法
【发布时间】:2014-11-12 04:39:43
【问题描述】:

我正在尝试在控制器操作中的 Promise 处理程序中使用 _super,但它不起作用,因为它似乎丢失了正确的函数链。

ApplicationRoute = Ember.Route.extend SimpleAuth.ApplicationRouteMixin,
  actions:
    sessionAuthenticationSucceeded: ->
      @get("session.user").then (user) =>
        if @get("session.isTemporaryPassword") or not user.get "lastLogin"
          @transitionTo "temp-password"
        else
          @_super()

我想在 else 上恢复为 Mixin 的默认行为,但我需要先异步解析 user,然后才能执行条件语句。我试过了:

ApplicationRoute = Ember.Route.extend SimpleAuth.ApplicationRouteMixin,
  actions:
    sessionAuthenticationSucceeded: ->
      _super = @_super
      @get("session.user").then (user) =>
        if @get("session.isTemporaryPassword") or not user.get "lastLogin"
          @transitionTo "temp-password"
        else
          _super()

ApplicationRoute = Ember.Route.extend SimpleAuth.ApplicationRouteMixin,
  actions:
    sessionAuthenticationSucceeded: ->
      @get("session.user").then (user) =>
        if @get("session.isTemporaryPassword") or not user.get "lastLogin"
          @transitionTo "temp-password"
        else
          @_super.bind(@)()

都不行。

This answer 声称这应该从 1.5.0 开始工作,但我使用的是 1.7.0-beta.5 并且不行。有没有办法让它发挥作用,即使是在不同的处理方式方面?

【问题讨论】:

    标签: ember.js coffeescript promise rsvp.js


    【解决方案1】:

    Ember 目前不支持异步调用 _super。在那个例子中,我实际上并没有异步调用_super,它仍然是同步的。

    http://emberjs.com/blog/2014/03/30/ember-1-5-0-and-ember-1-6-beta-released.html#toc_ever-present-_super-breaking-bugfix

    【讨论】:

      【解决方案2】:

      为了继续冒泡,您需要使用操作名称调用this.target.send()

      见:How can I bubble up an Ember action inside a callback function?

      这样的事情应该可以工作:

      ApplicationRoute = Ember.Route.extend SimpleAuth.ApplicationRouteMixin,
        actions:
          sessionAuthenticationSucceeded: ->
            @get("session.user").then (user) =>
              if @get("session.isTemporaryPassword") or not user.get "lastLogin"
                @transitionTo "temp-password"
              else
                @target.send('sessionAuthenticationSucceeded')
      

      【讨论】:

      • 嗯,这对我想要完成的工作有用吗?我想让默认行为(在 Mixin 代码中找到)在 else 上运行,而不是将操作冒泡到链中的不同类。
      猜你喜欢
      • 2019-08-30
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多