[英]EmberJS: How to transition to a router from a controller's action
我有一個動作:
{{action create target="controller"}}
我已將其定位到綁定控制器(而不是路由器),如下所示:
App.AddBoardController = Ember.Controller.extend
create: ->
App.store.createRecord App.Board, {title: @get "boardName"}
App.store.commit()
//TODO: Redirect to route
如何從控制器操作重定向回路由?
事實上,這不是Ember慣用語。 據我所知,以及我從Tom Dale自己學到的東西,這里有一些關於該代碼的評論:
你應該把動作放在路由器里面,並相應地轉換。
希望這會有所幫助。
UPDATE
第一個例子(靠近你的樣本)
在適當的路線:
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');
}
重構的例子
我建議你有以下路線:
show
:顯示現有項目, edit
:建議輸入項目的字段 進入封閉路徑,跟隨事件處理程序:
createItem
:創建一個新記錄並轉換edit
路徑,例如 editItem
:transitionTo edit
路線 進入edit
路徑,跟隨事件處理程序:
saveItem
:將提交store和transitionTo show
路由,例如 使用transitionToRoute('route')在Ember控制器操作中重定向:
App.AddBoardController = Ember.Controller.extend({
create: function(){
...
//TODO: Redirect to route
this.transitionToRoute('route_name');
}
...
編輯:繼續閱讀,邁克的答案討論了這種方法的一些問題。
您可以直接在路由器上調用transitionTo。 如果您使用默認值,則看起來像App.router.transitionTo('route', context)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.