如何使用Ember.js的新路由器以编程方式转换路由 [英] How to programmatically transition between routes using Ember.js' new Router
问题描述
如何使用新的Ember.js路由器以编程方式转换到新路由?
How do you do programmatically transition to a new route using the new Ember.js Router?
使用旧的Ember.js路由器,您可以使用路由器的发送$ c在路由/状态之间进行编程转换$ c>方法:
With the old Ember.js Router you could programmatically transition between routes/states using the router's send
method:
//OLD Router Syntax
App = Ember.Application.create({
Router: Ember.Router.extend({
root: Ember.Route.extend({
aRoute: Ember.Route.extend({
route: '/',
moveElsewhere: Ember.Route.transitionTo('bRoute')
}),
bRoute: Ember.Route.extend({
route: '/someOtherLocation'
})
})
})
});
App.initialize();
程序转换:
App.get('router').send('moveElsewhere');
给出新的Ember.js路由器(下面)我们如何完成同样的事情? >
Given the new Ember.js Router (below) how do we accomplish the same thing?
//NEW Router Syntax
App.Router.map(function(match) {
match('/').to('aRoute');
match('/someOtherLocation').to('bRoute');
});
解决方案(坏解决方案)
这是不对的,对吧?:
Work Around (Bad Solution?)
this can't be right, right?:
window.location = window.location.href + "#/someOtherLocation";
新路由器似乎无法使用的解决方案:
1)在 App.router上调用
实例发送
方法
Solutions that don't seem to work with the New Router:
1) calling the send
method on the App.router
instance
> App.router.send("moveElseWhere")
TypeError: Cannot call method 'send' of undefined
2)明确声明路由并设置事件
2) Explicitly declaring the Route and setting an event
App.ARoute = Ember.Route.extend({
events: {
moveElseWhere: function(context){
this.transitionTo('bRoute');
}
}
);
App.UploadRoute.moveElseWhere()
TypeError: Object (subclass of Ember.Route) has no method 'moveElseWhere'
注意:在撰写 Ember.js路由器文档仍然是指旧路由器,其中作为Ember.js路由器指南指新路由器
Note: At time of writing the Ember.js Router documentation still refers to the Old Router, where as the Ember.js Router guide refers to the new Router
推荐答案
假设此路由器定义:
App.Router.map ->
this.resource('old_route', {path : ""})
this.resource('new_route', {path : ":model_id"})
您可以使用 old_route.transitionToRoute(< )$>
从控制器
you can move to the new_route
with the old_route.transitionToRoute()
function when you have the controller as the context.
this.get('target').transitionToRoute('new_route', model_instance)
this.get('target')
- 从控制器返回当前路由器
this.get('target')
- returns the current route from the controller
this.get('controller').get('target').transitionToRoute('activity_detail', activity)
注意
函数* transitionTo ()已在1.0.0.RC3中已弃用
这篇关于如何使用Ember.js的新路由器以编程方式转换路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!