Ember.js:当链接到父资源时,如何在模型更改时留在Leaf Route中

时间:2014-06-06 18:49:22

标签: ember.js

我有一个帖子列表,包含嵌套摘要和完整路线。:

App.Router.map(function() {
  this.resource('post', {path: ':post_id'}, function() {
    this.route('summary', {path: '/'});
    this.route('full');
  });
});

摘要路线位于path: /,因此当我{{#link-to 'post' somePost}}时,它会到达post.summary

当我更改模型,即路由到someOtherPost时,我想留在当前的叶子路径(摘要或完整)。

我该怎么做?我尝试了willTransition,但我不确定如何检查模型是否发生变化:

这是迄今为止我所拥有的JSBin:http://emberjs.jsbin.com/benot/2/edit?js,output

1 个答案:

答案 0 :(得分:2)

这是动态链接的一个很好的用例。

link-to Code

App.ApplicationController = Em.ArrayController.extend({
  postLink: 'post'
});

模板

{{#each item in controller}}
  <li>{{#link-to postLink item}}{{item.title}}{{/link-to}}</li>
{{/each}}

您可以从每个子路由发送一个操作,它将在路由器(http://emberjs.com/guides/templates/actions/)上移动直到被捕获。在父路线上,你可以改变link-to和bam的动态值,你很高兴。

路线代码

App.PostSummaryRoute = Em.Route.extend({
  actions:{
    didTransition: function(){
      this.send('swapLink', 'post.summary');
    }
  }
});

App.PostFullRoute = Em.Route.extend({
  actions:{
    didTransition: function(){
      this.send('swapLink', 'post.full');
    }
  }
});


App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return [
      App.Post.create({id: '1', title: 'Foo', body: 'Lorem'}),
      App.Post.create({id: '2', title: 'Bar', body: 'Ipsum'})
    ];
  },
  actions: {
    swapLink: function(newLink){
      console.log('swapLink ' + newLink);
      this.controller.set('postLink', newLink);
    }
  }
});

http://emberjs.jsbin.com/dekokafu/1/edit