【问题标题】:Selectively trigger redirect选择性触发重定向
【发布时间】:2013-11-16 07:05:45
【问题描述】:

这是我的路由器的一部分:

this.resource('nodes', function () {

    this.resource('allNodes', function () {
        this.route('index');
        this.route('new');
        this.route('show',   { path: '/:node_id/show' });
        this.route('edit',   { path: '/:node_id/edit' });
        this.route('delete', { path: '/:node_id/delete' });
    });

    this.resource('services', function () {
        this.route('index');
        this.route('new');
        this.route('show',   { path: '/:service_id/show' });
        this.route('edit',   { path: '/:service_id/edit' });
        this.route('delete', { path: '/:service_id/delete' });
    });
...

/nodes 路由中,我重定向到allNodes.index

SettingsApp.NodesRoute = Ember.Route.extend({
    redirect: function () {
        console.log('NodesRoute.redirect > redirecting to %s', ROUTES.ALL_NODES);
        this.transitionTo(ROUTES.ALL_NODES);
    }
});

现在在我的应用程序的其他部分中,我直接使用 url 链接到服务:

#/nodes/services/71ae38cde8584dc9c4eea25e74e68310/show

但这不起作用,因为/nodes 的重定向正在启动。我该如何防止这种情况?

重定向必须在转到/nodes 时处于活动状态,而不是任何子网址。我必须在重定向之前通过检查currentPath 手动实现此过滤,还是有标准的方法来执行此操作?

【问题讨论】:

    标签: ember.js url-routing


    【解决方案1】:

    NodesIndexRoute 中进行重定向。

     App.NodesIndexRoute = Ember.Route.extend({
       beforeModel: function () {
        this.transitionTo('allnodes');
       }
     });
    

    Working Model

    【讨论】:

    • 是的,这行得通!谢谢! (抱歉回复晚了)
    【解决方案2】:

    您需要手动实现它(虽然信息在钩子中提供)

    此外,redirect 是一个已弃用的钩子,取而代之的是 afterModel 钩子。

    SettingsApp.NodesRoute = Em.Route.extend({
      afterModel: function(resolvedModel, transition){
         if(transition.targetName == 'nodes'){
            this.transitionTo(ROUTES.ALL_NODES);
         }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2014-08-09
      • 2021-04-11
      • 2011-11-25
      相关资源
      最近更新 更多