Eva*_* R. 10 ember.js ember-router
如何使用新的Ember.js路由器以编程方式转换到新路由?
使用旧的Ember.js路由器,您可以使用路由器的send方法以编程方式在路由/状态之间转换:
//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();
Run Code Online (Sandbox Code Playgroud)
程序转换:
App.get('router').send('moveElsewhere');
Run Code Online (Sandbox Code Playgroud)
鉴于新的Ember.js路由器(下面)我们如何完成同样的事情?
//NEW Router Syntax
App.Router.map(function(match) {
match('/').to('aRoute');
match('/someOtherLocation').to('bRoute');
});
Run Code Online (Sandbox Code Playgroud)
这不对,对吧?:
window.location = window.location.href + "#/someOtherLocation";
Run Code Online (Sandbox Code Playgroud)
1)send在App.router实例上调用方法
> App.router.send("moveElseWhere")
TypeError: Cannot call method 'send' of undefined
Run Code Online (Sandbox Code Playgroud)
2)明确声明路线并设置事件
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'
Run Code Online (Sandbox Code Playgroud)
注意:在编写本文时,Ember.js路由器文档仍然引用旧路由器,其中Ember.js路由器指南引用新路由器
假设此路由器定义:
App.Router.map ->
this.resource('old_route', {path : ""})
this.resource('new_route', {path : ":model_id"})
Run Code Online (Sandbox Code Playgroud)
将控制器作为上下文时,可以new_route使用该old_route.transitionToRoute()功能移动到该功能.
this.get('target').transitionToRoute('new_route', model_instance)
Run Code Online (Sandbox Code Playgroud)
this.get('target') - 从控制器返回当前路径
this.get('controller').get('target').transitionToRoute('activity_detail', activity)
Run Code Online (Sandbox Code Playgroud)
函数*transitionTo()已在1.0.0.RC3中弃用
您可以使用 {{linkTo}} 帮助程序触发指向新路线的链接:
#your template
{{#linkTo allTodos activeClass="selected"}}All{{/linkTo}}
#your router
App.Router.map(function (match) {
match("/").to("todos", function (match) {
match("/").to("allTodos"); // will fire this router
match("/active").to("activeTodos");
match("/completed").to("completedTodos");
});
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :)
| 归档时间: |
|
| 查看次数: |
15963 次 |
| 最近记录: |