[英]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自己学到的东西,这里有一些关于该代码的评论:
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.