Ember.js: 从视图中判断当前路由
Ember.js: Determine current route from the view
我有两条路线,一条是未读的书,一条是已读的书。两条路线共享相同的模板:
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}}
在 controller
中设置 属性 而不是 route
。
App.UserPanelBooksIndexController = Ember.Controller.extend({
readBooks: false
});
我把东西放在一起here
如果它是索引,您可能需要扩展 Ember.ArrayController
而不是通用的 Ember.Controller
你也可以使用路由的 setupController 钩子。你的路线看起来像。
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);
}
});
我有两条路线,一条是未读的书,一条是已读的书。两条路线共享相同的模板:
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}}
在 controller
中设置 属性 而不是 route
。
App.UserPanelBooksIndexController = Ember.Controller.extend({
readBooks: false
});
我把东西放在一起here
如果它是索引,您可能需要扩展 Ember.ArrayController
而不是通用的 Ember.Controller
你也可以使用路由的 setupController 钩子。你的路线看起来像。
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);
}
});