Ember.js:transitionTo路由,然后到动态段 [英] Ember.js: transitionTo route, then to dynamic segment

查看:347
本文介绍了Ember.js:transitionTo路由,然后到动态段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个路由器设置了帐户和帐户/:account_id选项。当用户登陆我的应用程序的索引页面时,我将其转换到帐户路由。

  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');
}
});

该文档给出详细信息,但不提供示例,仅提供以下内容: p>


transitionTo(name,models)



另一条路线。可选地为路由
提供模型。我将尝试以下操作:

  this.transitionTo('accounts / 4'); 
未捕获错误:断言失败:没有找到路由帐户/ 4

this.transitionTo('accounts',Social.Account.find(1));
未捕获比动态段更多的对象被传递


解决方案

我把他人的答案放在一起,并发现这个答案:



定义你的路线,如:

  this.resource('accounts',function(){
this.route('account',{path:'/:account_id '});
});

重定向:

  this.transitionTo('accounts.account',accountObj); 

但是,如果从服务器加载,则需要 accountObj object redirect before redirect:

  var accountObj = App.Account.find 1); 
accountObj.one('didLoad',this,function(){
this.transitionTo('accounts.account',accountObj);
});

我设置了这个小提琴有完整的例子


I have a Router set up with accounts, and account/:account_id options. When the user lands on the index page of my app I transition them to the accounts route.

Social.Router.map(function() {
    this.resource('accounts', function(){
        this.resource('account', { path: ':account_id'});
    });
});

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts');
    }
});

What I'd like to do is to transition them to a specified :account_id route based on some criteria. At the moment I just want to get the first account in the array and use that. But in the future it could be a way to transition them to the last account they were viewing. Something like this:

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts/:account_id');
    }
});

The docs give "detail" but don't provide an example, only offering the following:

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.

I've tried the following:

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

解决方案

I put together others' answers and some fiddling and came out with this answer:

define your routes like:

this.resource('accounts', function () {
    this.route('account', {path: '/:account_id'});
});

redirect:

this.transitionTo('accounts.account', accountObj);

But if you are loading from server, you need the accountObj object loaded before redirect:

var accountObj = App.Account.find(1);
accountObj.one('didLoad', this, function () {
    this.transitionTo('accounts.account', accountObj);
});

I set up this fiddle with complete example

这篇关于Ember.js:transitionTo路由,然后到动态段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆