【问题标题】:Understanding Router Resources in Ember.js了解 Ember.js 中的路由器资源
【发布时间】:2014-04-23 15:53:51
【问题描述】:

假设我的 Ember.js 应用有一个如下所示的路由器:

App.Router.map(function () {
  this.resource('posts');
});

我了解from the guide 这意味着我有两条路线:PostsRoutePostsIndexRoute,而PostsRoute 是父路线。

这是我的PostsRoute

App.PostsRoute = Em.Route.extend({
  model: function () {
    this.store.find('posts');
  },

  afterModel: function (posts) {
    var promises = posts.map(function (post) {
      return post.get('author'); // some async relationship
    });

    return Em.RSVP.Promise.all(promises);
  }
});

还有我的PostsIndexRoute

App.PostsIndexRoute = Em.Route.extend({
  model: function () {
    return this.modelFor('posts');
  }
});

由于我的PostsRoutePostsIndexRoute 的父级,我希望它的model 挂钩始终得到解决,即使从我的应用程序中的其他位置转换到“帖子”(例如通过transitionTolink-to)。情况似乎并非如此。 <Resource><Resource>Index 路由之间究竟是什么关系?我有点困惑为什么你需要两者。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    为了让 Ember 自动调用 PostsIndexRoutemodel 挂钩,您必须使用第二个参数声明您的资源。有第二个参数表示它应该有子路由(index 是默认提供的)。

    App.Router.map(function(){
      this.resource('posts', function(){});
    })
    

    Here's a bin 演示了该行为。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2015-10-15
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    相关资源
    最近更新 更多