2013-11-01 26 views
5

比方说,我有一个ApplicationRoute的动作goBack(如您在评论中看到的,由于不同移动浏览器中的错误,我需要自行处理goBack):Ember.js:从嵌套路由中调用ApplicationRoute的动作

Mobile.ApplicationRoute = Em.Route.extend 
    actions: 
     goBack: -> 
      # TODO: Remove when iOS 7 fixed their history 
      # If there's no route to go back, go to front 
      # TODO: Remove when Microsoft fixed their 
      # back button in offline mode 
      if not (Nn.MobileHelper.isiPhone() or Nn.MobileHelper.isIeMobile()) and @get("router.recentRoute")? 
       return window.history.back() 

      @get("controller").set("isHitBackButton", true) 

      @transitionTo("front").then => @get("controller").set("isHitBackButton", false) 

如何从其他路线触发此操作?请注意,由于我需要拨打@transitionTo,这段代码必须在路由中。

回答

5

默认情况下,操作会触发应用程序路由!只需在您的模板中使用{{action 'goBack'}},或从代码(减号组件)调用this.send('goBack')即可。从组件中您需要连接事件调用有点不同,并使用this.sendAction('internalActionName')

http://emberjs.jsbin.com/ulIhUze/1/edit