2014-10-20 22 views
5

我环顾四周,但我找不到任何良好的文档的实际区别如下之间是什么:EmberJS中的[],@each,content和<arrayName>有什么区别?

Ember.Object.extend({ 
    // ... 
    myProperty1: function() { /* ... */ }.property('myArray'), 
    myProperty2: function() { /* ... */ }.property('myArray.content'), 
    myProperty3: function() { /* ... */ }.property('myArray.[]'), 
    myProperty4: function() { /* ... */ }.property('[email protected]') 
}); 

我也明白,.content似乎是数组的属性内部存储,这可能无法使用,如果这恰好是PromiseArray。我也明白,@each不会以这种方式使用,但主要是为了访问ProxyArray,由于映射此数组中每个元素的内部属性而生成结果。

除了这些微妙的差异,他们似乎工作几乎相同。但是myArraymyArray.[]呢?他们和其他人相比如何呢?

回答

3
Ember.Object.extend({ 
    // ... 

    // Updates if myArray is set to a new value using .set 
    myProperty1: function() { /* ... */ }.property('myArray'), 

    // Updates if myArray is an Ember Object (like an ArrayController) 
    // and its 'content' property is set to a new value using 
    // .set('content', newArray) or .replaceContent(...) 
    // which can also happen implicitly 
    // Also note that for an ArrayController 'content' is an alias of 'model' 
    myProperty2: function() { /* ... */ }.property('myArray.content'), 

    // Updates if myArray is an Ember Array or ArrayProxy or MutableEnumerable 
    // and it is 'mutated' using myArray.addObject(s), myArray.removeObject, 
    // myArray.popObject, myArray.shiftObject, myArray.pushObject(s), etc. 
    myProperty3: function() { /* ... */ }.property('myArray.[]'), 

    // Updates if myArray is an Ember Array or ArrayProxy and one of it's 
    // elements is set to a new value using .set or .replace 
    // such as this.set('myArray.firstObject', 10) or 
    // this.get('myArray').replace(2, 1, [10]); 
    myProperty4: function() { /* ... */ }.property('[email protected]') 
}); 

我也会注意,如果您忘记使用的Ember的方法之一,简单地更新使用简单的JavaScript像这样的数组:

this.myArray = []; 

这些都不计算特性将被更新。

+0

令人印象深刻的答案。直截了当,明确。非常感谢你。 – Alpha 2014-10-21 00:54:03

相关问题