I want to implement computed property in controller that changes when data in route's model changed.
我想在控制器中实现计算属性,该属性在路由模型中的数据发生更改时发生更改。
Route:
路线:
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return new Ember.RSVP.hash({
ingredients: this.store.findAll('ingredient'),
recipes: this.store.peekAll('recipe')
});
},
setupController: function(controller, modelHash) {
controller.setProperties(modelHash);
}
});
Controller:
控制器:
import Ember from 'ember';
export default Ember.Controller.extend({
pageNumber: 0,
pageSize: 16,
pages: function() {
var pages = [];
if (this.model != null) {
var content = this.model.recipes;
while (content.length > 0) {
pages.push(content.splice(0, this.get("pageSize")));
}
}
return pages;
}.property('model.recipes.@each', 'pageSize'),
recipesOnPage: function() {
return this.get('pages')[this.get('pageNumber')];
}.property('pages', 'pageNumber')
});
This code produce no error, but doesn't work - "pages" always empty. And "pages" property doesn't recomputed on model changing. What am I doing wrong? And how to achieve desired result?
此代码不会产生错误,但不起作用 - “pages”始终为空。并且“页面”属性不会在模型更改时重新计算。我究竟做错了什么?以及如何达到预期的效果?
P.S. Ember version - 1.13.
附:灰烬版 - 1.13。
2 个解决方案
#1
1
Since you have modified setupController
hook, your controller
has properties ingredients
and recipes
, but has no model
property.
由于您已修改了setupController挂钩,因此您的控制器具有属性成分和配方,但没有模型属性。
So your computed property should be:
所以你的计算属性应该是:
pages: function() {
// avoid using model here
// use this.get('recipes') instead of this.model.recipes
}.property('recipes.[]', 'pageSize')
SetupController hook guides link.
SetupController钩子引导链接。
#2
0
Please try:
请尝试:
import Ember from 'ember';
export default Ember.Controller.extend({
pageNumber: 0,
pageSize: 16,
pages: function() {
var pages = [];
var model = this.get('model');
if (model != null) {
var content = model.get('recipes');
while (content.length > 0) {
pages.push(content.splice(0, this.get("pageSize")));
}
}
return pages;
}.property('model.recipes.@each', 'pageSize'),
recipesOnPage: function() {
return this.get('pages')[this.get('pageNumber')];
}.property('pages', 'pageNumber')
});
#1
1
Since you have modified setupController
hook, your controller
has properties ingredients
and recipes
, but has no model
property.
由于您已修改了setupController挂钩,因此您的控制器具有属性成分和配方,但没有模型属性。
So your computed property should be:
所以你的计算属性应该是:
pages: function() {
// avoid using model here
// use this.get('recipes') instead of this.model.recipes
}.property('recipes.[]', 'pageSize')
SetupController hook guides link.
SetupController钩子引导链接。
#2
0
Please try:
请尝试:
import Ember from 'ember';
export default Ember.Controller.extend({
pageNumber: 0,
pageSize: 16,
pages: function() {
var pages = [];
var model = this.get('model');
if (model != null) {
var content = model.get('recipes');
while (content.length > 0) {
pages.push(content.splice(0, this.get("pageSize")));
}
}
return pages;
}.property('model.recipes.@each', 'pageSize'),
recipesOnPage: function() {
return this.get('pages')[this.get('pageNumber')];
}.property('pages', 'pageNumber')
});