Ember.js:从视图中确定当前路线

时间:2015-01-11 01:58:48

标签: javascript ember.js ember-data

我有两条路线,一条用于未读书籍,另一条用于阅读书籍。两条路由共享相同的模板:

App.UserPanelBooksIndexRoute = Ember.Route.extend({
  templateName: 'userPanel/index',
  model: function() {
    return this.store.filter("book", function(book) { return !book.get("isRead"); });
  },
  readBooks: false
});

App.UserPanelBooksSoldRoute = Ember.Route.extend({
  templateName: 'userPanel/index',
  model: function() {
    return this.store.filter("book", function(book) { return book.get("isRead"); });
  },
  readBooks: true
});

在视图中,我会渲染图书,如果没有未读的图书,我想显示"Sorry, no unread books",如果没有阅读图书,我想显示"Sorry, no read books"消息。除了我在路线中设置布尔值的方法不起作用(readBooks在视图中始终评估为真):

{{#if model.length}}
  {{#each book in model itemController='book'}}
    <ul>{{book.title}}</ul>
  {{/each}}
{{else}}
  {{#if readBooks}}
    <h1>Sorry, no sold books</h1>
  {{else}}
    <h1>Sorry, no unsold books</h1>
  {{/if}}
{{/if}}

2 个答案:

答案 0 :(得分:1)

controller中设置属性,而不是route

App.UserPanelBooksIndexController = Ember.Controller.extend({
  readBooks: false
});

我把东西放在一起here

如果它是一个索引,您可能需要扩展Ember.ArrayController而不是通用Ember.Controller

答案 1 :(得分:0)

你也可以使用routeController的路由钩子。你的路线看起来像。

App.UserPanelBooksIndexRoute = Ember.Route.extend({
  templateName: 'userPanel/index',
  model: function() {
    return this.store.filter("book", function(book) { return !book.get("isRead"); });
  },
  setupController(controller,model){
  this._super(...arguments);
   controller.set('readBooks',false);
  }
});

App.UserPanelBooksSoldRoute = Ember.Route.extend({
  templateName: 'userPanel/index',
  model: function() {
    return this.store.filter("book", function(book) { return book.get("isRead"); });
  },
  setupController(controller,model){
   this._super(...arguments);
   controller.set('readBooks',true);
  }
});