javascript EmberJS:如何从控制器的动作转换到路由器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11552417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EmberJS: How to transition to a router from a controller's action
提问by Charlie
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?
如何从控制器操作重定向回路由?
回答by HaoQi Li
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');
}
...
回答by Mike Aski
In fact, this is not Ember idiomatic. From what I know, and what I have learnt from Tom Dale himself, here are some remarks about that code:
事实上,这不是 Ember 惯用的。据我所知,以及我从 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. 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.
- 首先,您不应该从路由器内部以外的其他地方转换:这样做,您将面临严重的问题,因为您不知道路由器处于哪种状态,因此为了保持运行,您将很快不得不降低您的性能设计,顺便说一下你代码的整体质量,最后是你的应用程序的稳定性,
- 其次,您显示的操作内容应该位于路由器内部以避免不需要的上下文执行。路由器确实是一种为整个应用程序强制执行一致行为的方法,仅在某些状态下处理操作。当您将操作实现放入控制器时,可以随时调用这些操作,包括错误的...
- 最后,Ember 的控制器并不旨在包含行为,因为它们是增值包装器,主要保存计算属性。如果你仍然想要分解原语,也许模型可以是一个好地方,或者第三方上下文,但肯定不是控制器。
You should definitely put the action inside the router, and transitionTo accordingly.
您绝对应该将操作放在路由器中,并相应地转换为。
Hope this will help.
希望这会有所帮助。
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,edit
: proposes to input item's fields
show
:显示现有项目,edit
: 建议输入项目的字段
Into the enclosing route, following event handlers:
进入封闭路线,以下事件处理程序:
createItem
: create a new record and transitionToedit
route, e.geditItem
: transitionToedit
route
createItem
: 创建一个新记录和 transitionToedit
路由,例如editItem
:过渡到edit
路线
Into the edit
route, following event handlers:
进入edit
路由,以下事件处理程序:
saveItem
: which will commit store and transitionToshow
route, e.g
saveItem
: 这将提交 store 和 transitionToshow
路由,例如
回答by Bradley Priest
EDIT: Keep reading, Mike's answer discusses some of the problems with this approach.
编辑:继续阅读,迈克的回答讨论了这种方法的一些问题。
You can just call transitionTo directly on the router. If you are using defaults this looks like App.router.transitionTo('route', context)
.
您可以直接在路由器上调用 transitionTo 。如果您使用默认值,这看起来像App.router.transitionTo('route', context)
.