简体   繁体   English

EmberJS:如何从控制器的动作转换到路由器

[英]EmberJS: How to transition to a router from a controller's action

I have an action: 我有一个动作:

{{action create target="controller"}}

which I have targeted to the bound controller (rather than the router) like this: 我已将其定位到绑定控制器(而不是路由器),如下所示:

App.AddBoardController = Ember.Controller.extend
    create: ->
        App.store.createRecord App.Board, {title: @get "boardName"}
        App.store.commit()
        //TODO: Redirect to route

How do I redirect back to a route from the controller action? 如何从控制器操作重定向回路由?

In fact, this is not Ember idiomatic. 事实上,这不是Ember惯用语。 From what I know, and what I have learnt from Tom Dale himself, here are some remarks about that code: 据我所知,以及我从Tom Dale自己学到的东西,这里有一些关于该代码的评论:

  • First, you should not transitionTo from elsewhere than inside the router: by doing so, you are exposing yourself to serious issues as you don't know in which state is the router, so to keep stuff running, you will quickly have to degrade your design, and by the way the overall quality of you code, and finally the stability of your app, 首先,你不应该从路由器内部转移到其他地方:通过这样做,你会暴露自己的严重问题,因为你不知道路由器在哪个状态,所以为了保持运行,你很快就会降低你的设计,顺便提一下代码的整体质量,最后是应用程序的稳定性,
  • Second, the action content you are showing should be located inside the router to avoid undesired context execution. 其次,您显示的操作内容应位于路由器内部,以避免意外的上下文执行。 The router is indeed a way to enforce a coherent behavior for the whole app, with actions being processed only in certain states. 路由器确实是一种强制整个应用程序连贯行为的方法,只在某些状态下处理操作。 While you are putting the actions implementation into Controllers, those actions can be called at anytime, any including wrong... 当您将动作实现放入控制器时,可以随时调用这些动作,包括任何错误...
  • Finally, Ember's controllers are not aimed to contain behavior as they rather are value-added wrappers, holding mainly computed properties. 最后,Ember的控制器并不旨在包含行为,因为它们更像是增值包装器,主要包含计算属性。 If you nevertheless want to factorize primitives, maybe the model can be a good place, or a third party context, but certainly not the Controller. 如果你想要对基元进行因子分解,那么模型可能是一个好地方,或者是第三方环境,但肯定不是控制器。

You should definitely put the action inside the router, and transitionTo accordingly. 你应该把动作放在路由器里面,并相应地转换。

Hope this will help. 希望这会有所帮助。

UPDATE UPDATE

First example (close to your sample) 第一个例子(靠近你的样本)

In the appropriated route: 在适当的路线:

saveAndReturnSomewhere: function (router, event) {
  var store = router.get('store'),
      boardName = event.context; // you pass the (data|data container) here. In the view: {{action saveAndReturnSomewhere context="..."}}
  store.createRecord(App.Board, {
    title: boardName
  });
  store.commit();
  router.transitionTo('somewhere');
}

Refactored example 重构的例子

I would recommend having the following routes: 我建议你有以下路线:

  • show : displays an existing item, show :显示现有项目,
  • edit : proposes to input item's fields edit :建议输入项目的字段

Into the enclosing route, following event handlers: 进入封闭路径,跟随事件处理程序:

  • createItem : create a new record and transitionTo edit route, eg createItem :创建一个新记录并转换edit路径,例如
  • editItem : transitionTo edit route editItem :transitionTo edit路线

Into the edit route, following event handlers: 进入edit路径,跟随事件处理程序:

  • saveItem : which will commit store and transitionTo show route, eg saveItem :将提交store和transitionTo show路由,例如

Use transitionToRoute('route') to redirect inside an Ember controller action: 使用transitionToRoute('route')在Ember控制器操作中重定向:

App.AddBoardController = Ember.Controller.extend({
    create: function(){ 
        ...
        //TODO: Redirect to route
        this.transitionToRoute('route_name');
    }
...

EDIT: Keep reading, Mike's answer discusses some of the problems with this approach. 编辑:继续阅读,迈克的答案讨论了这种方法的一些问题。

You can just call transitionTo directly on the router. 您可以直接在路由器上调用transitionTo。 If you are using defaults this looks like App.router.transitionTo('route', context) . 如果您使用默认值,则看起来像App.router.transitionTo('route', context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM