【问题标题】:Ember Router - determine if route/resource is child of current routeEmber 路由器 - 确定路由/资源是否是当前路由的子路由
【发布时间】:2015-01-19 22:21:33
【问题描述】:

有没有办法确定给定路线是否是当前路线的子路线?

我在 Route 中使用 willTransition 挂钩来确定当用户离开此路由时是否应该显示数据丢失确认消息。如果用户在此资源的路由器映射中包含的各种子资源和路由之间进行转换,我不希望执行此数据丢失确认,但我找不到方法来确定我在 willTransition 中给出的转换是否是这个.routeName。

有什么建议吗?

路由器:

Router.map(function() {
   this.route('some-route')
   this.resource('parent-resource', function() {
     this.route('child-route');
     this.resource('child-resource', function() {
       this.route('child-resource-route1');
       this.route('child-resource-route2');
     }
   }
});

ParentResourceRoute:

Ember.Route.extend({
   actions: {
      willTransition: function(transition) {
        // if transition is not a child of this.routeName display a confirmation alert if there is dirty data 
      }
   }
});

【问题讨论】:

  • 我不会把它放在 willTransition 中,我宁愿在父路由中创建一个子路由(知道发生了什么的路由)可以调用以显示警报的方法。
  • 我不太确定在这种情况下是否可行。在子路由之间转换时,我不希望发生任何确认。我唯一关心确定是否存在脏数据的时间是离开父资源路由并转到其兄弟姐妹之一时。如果我从子路由进行检查,我怎么知道我是否要在父资源之外转换?

标签: javascript ember.js ember-router


【解决方案1】:

怎么样

Ember.Route.extend({
 actions: {
  willTransition: function(transition) {
    if (transition.get('targetName').indexOf(this.get('routeName')) === false) {
      // moving to a sibling or higger route
    }
  }
 }
});

可能需要根据您的路线名称进行调整。

【讨论】:

  • 如果我只有一层嵌套的子路由,那效果很好——我的例子中的 parent-resource.child-route 就可以了。但是,一旦我嵌套了资源,它就会崩溃,因为它们不会包含 targetName 中最顶层的父资源。在这种情况下,我的叶子路由将类似于 child-resource.child-resource-route1。
  • indexOf(anything) === false 实际上永远不会工作。找不到任何东西将返回 -1
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 2019-10-18
  • 2015-10-15
  • 2011-01-17
  • 2011-07-05
  • 1970-01-01
  • 2016-04-11
相关资源
最近更新 更多