Ember.js:transitionTo路由,然后到动态段
我有一个路由器设置帐户和帐户/:account_id选项。当用户登录到我的应用程序的索引页面时,我将它们转换为帐户路线。Ember.js:transitionTo路由,然后到动态段
Social.Router.map(function() {
this.resource('accounts', function(){
this.resource('account', { path: ':account_id'});
});
});
Social.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('accounts');
}
});
我想要做的就是根据一些标准将它们转换到指定的:account_id路由。目前我只想获得阵列中的第一个帐户并使用它。但是将来它可能是一种将他们转换到他们正在查看的最后一个帐户的方式。事情是这样的:
Social.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('accounts/:account_id');
}
});
的文档给 “细节”,但没有提供一个例子,仅提供了以下内容:
transitionTo (name, models)
Transition into another route. Optionally supply a model for the route in question. The model will be serialized into the URL using the serialize hook.
我已经试过如下:
this.transitionTo('accounts/4');
Uncaught Error: assertion failed: The route accounts/4 was not found
this.transitionTo('accounts', Social.Account.find(1));
Uncaught More objects were passed than dynamic segments
我放在一起别人的答案和一些摆弄和这个答案出来了:
这样定义你的路由:
this.resource('accounts', function() {
this.route('account', {path: '/:account_id'});
});
重定向:
this.transitionTo('accounts.account', accountObj);
但如果从服务器加载,则需要加载accountObj
对象b安伏重定向:
var accountObj = App.Account.find(1);
accountObj.one('didLoad', this, function() {
this.transitionTo('accounts.account', accountObj);
});
我成立了this fiddle与完整的例子
它看起来像transitionTo不赞成使用transitionToRoute。
尽管如此,您可以通过使原始声明为this.resource('account', { path: '/:account_id'});
,然后使用单个创建的对象进行转换来实现重新路由。
您没有正确指定路径路径,您应该有资源下的路线,而不是另一个资源。它应该是这样的:
Social.Router.map(function() {
this.resource('accounts', function(){
this.route('account', { path: '/:account_id'});
});
});
Social.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('accounts.account', Social.Account.find(1));
}
});
好。实现这个代码让我有两件事。 1)URL按预期变化。 2)我得到一个控制台错误'未捕获的错误:断言失败:没有找到路由帐户。 当我登录'/'时,URL变为'/#/ accounts/1',这是我所期望的,但没有加载,并且出现控制台错误。 – commadelimited 2013-02-27 21:37:23
我忘了将路径添加到根帐户资源。 – buuda 2013-02-28 15:08:26
由于您的/:account_id是您需要transitionToRoute'帐户'的资源。您也需要相应的AccountRoute。
如果/:account_id是一个路径而不是资源,你会transitionToRoute'accounts.account',你的路由处理器将被称为AccountsAccountRoute。
随着最新的灰烬(1.0.0-RC-6),这完美的作品。
路由器:
this.resource('accounts', function() {
this.resource('account', {path: '/:account_id'});
});
重定向:
this.transitionToRoute('account', Social.Account.find(1))
它只适用于烬1.1.3 + pre.e0ffbf84。谢谢。 – hiroshi 2013-11-20 13:30:15
你的小提琴似乎没有工作,所以我用来自CDNjs.com的电话进行了更新:http://jsfiddle.net/XNyme/9/ – commadelimited 2013-02-28 21:31:20