我有一个组件,当我使用时会引发错误observes
,但是当我property
在displayPages
计算属性中使用时不会引发错误:
App.ExampleComponent = Ember.Component.extend({
tagName : 'ul',
classNames : [ 'pagination' ],
current : 1,
modelName : null,
meta : null,
actions : {
next : function() {
var current = this.get('current');
this.set('current', parseInt(current, 10) + 1);
},
previous : function() {
var current = this.get('current');
this.set('current', parseInt(current, 10) - 1);
}
},
updateMeta : function() {
this.set('meta', new Number(Math.random()));
}.observes('current').on('init'),
currentPage : function() {
return Number(this.get('current'));
}.property('current'),
displayPages : function() {
// complex logic lives here, so I used "observes" instead of "property"
console.log('Im invoked the first time meta is set, but not on subsequent changes');
var result = [];
return result;
}.property('meta')
});
如果我更改observes
并使用property
它,则可以正常工作。但是该displayPages
属性包含复杂的逻辑,因此我想observes
按照文档指南使用:
http://emberjs.com/guides/object-model/what-do-i-use-when/
如果.property('meta')
将其替换并替换为.observes('meta')
,则会出现以下错误:
Uncaught Error: Assertion Failed: ArrayProxy expects an Array or Ember.ArrayProxy, but you passed function
问题是……为什么呢?
我不认为我的理解之间的不同observes
和property
,但阅读文档并没有太大的帮助在这种情况下..
计算的属性返回一个值。值更改时观察触发功能。换句话说,观察值不返回值。
您可能正在尝试迭代自己的observes方法,Ember大喊大叫,我希望数组能够进行迭代,并且您向我传递了一个函数。
显示此示例:http : //emberjs.jsbin.com/tariliya/1/edit
您应该使用计算属性。每当相关属性更改时,它都会更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句