2014-10-20 7 views
5

Ho guardato in giro, ma non riesco a trovare nulla di buono documentazione su quali siano le differenze reali sono tra i seguenti:Qual è la differenza tra [], @each, contenuto e <arrayName> in EmberJS?

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

Capisco che .content sembra essere la memoria interna della matrice per la proprietà, che potrebbe non essere disponibile se questo è un PromiseArray. Capisco anche che @each non sia usato in questo modo, ma principalmente per accedere a un ProxyArray che genera risultati come risultato del mapping delle proprietà interne di ciascuno degli elementi in questo array.

Oltre a queste sottili differenze, sembrano funzionare praticamente allo stesso modo. Ma che dire di myArray e myArray.[]? E che dire di loro in contrasto con il resto?

risposta

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]') 
}); 

sarò anche notare che, se si dimentica di usare uno dei metodi di Ember e semplicemente aggiornare la matrice utilizzando semplici JavaScript in questo modo:

this.myArray = []; 

nessuna di queste proprietà calcolate sarà aggiornato.

+0

Risposta impressionante. Dritto al punto e chiaro. Grazie mille. – Alpha