【问题标题】:Ember.js: transitionTo route, then to dynamic segmentEmber.js:transitionTo 路由,然后到动态段
【发布时间】:2013-02-26 22:19:57
【问题描述】:

我有一个设置了帐户的路由器,以及 account/: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');
    }
});

文档给出了“详细信息”,但没有提供示例,仅提供以下内容:

transitionTo(名称、型号)

过渡到另一条路线。可选择为路线提供模型 有问题。该模型将使用 序列化钩子。

我尝试了以下方法:

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

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    我整理了别人的答案和一些摆弄,得出了这个答案:

    定义您的路线,例如:

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

    重定向:

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

    但是如果你从服务器加载,你需要在重定向之前加载accountObj 对象:

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

    我用完整的例子设置了this fiddle

    【讨论】:

    • 你的小提琴似乎不起作用,所以我用来自 CDNjs.com 的调用更新了它:jsfiddle.net/XNyme/9
    【解决方案2】:

    看来,transitionTo 已被弃用,取而代之的是 transitionToRoute。

    尽管如此,您可以通过将原始声明设置为 this.resource('account', { path: '/:account_id'}); 来实现重新路由,然后使用单个创建的对象进行转换。

    【讨论】:

      【解决方案3】:

      您没有正确指定路由路径,您应该在资源下有一个路由,而不是另一个资源。应该是这样的:

      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) 我收到Uncaught Error: assertion failed: The route account was not found 的控制台错误。当我登陆/ 时,URL 会更改为/#/accounts/1,这是我所期望的,但没有加载任何内容并且出现控制台错误。
      • 我忘记添加根帐户资源的路径。
      【解决方案4】:

      使用最新的 Ember (1.0.0-RC-6),可以完美运行。

      路由器:

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

      重定向:

      this.transitionToRoute('account', Social.Account.find(1))
      

      【讨论】:

      • 它只适用于 ember 1.1.3+pre.e0ffbf84。谢谢。
      【解决方案5】:

      由于您的 /:account_id 是您需要 transitionToRoute 'account' 的资源。您还需要相应的 AccountRoute。

      如果 /:account_id 是路由而不是资源,您将 transitionToRoute 'accounts.account' 并且您的路由处理程序将称为 AccountsAccountRoute。

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 2014-07-05
        • 2022-11-12
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多