2014-10-20 19 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

相關問題