samedi 3 janvier 2015

Em.Computed.sort triggers computed property twice

One of the components in my app contains an array that is copied, filtered, sorted, sliced and then finally looped through. I'm having problems with the Em.computed.sort() part which seems to trigger another computed property to run twice (which then causes a bunch of other issues in my app).



{{#each item in final}}
<li>{{item.name}} -- {{item.age}}</li>
{{/each}}

<button {{action "changeModel"}}>change model</button>


.



// initial property copied
initial: function(){
return this.get('model').slice()
}.property('model'),

// sorted
sortProp: ['age:desc'],
sorted: Ember.computed.sort('initial', 'sortProp'),

// slice it and then loop through it - this function runs twice
final: Ember.computed('sorted.[]', function(){
return this.get('sorted').slice(0, 5)
}),


I have a simplified version of my issue - see my jsbin.


Basically, when the model changes, the final() function runs twice - once containing the old model data, and a second with the new, updated model data. Both times, the initial data is correct, it's just the Em.computed.sort data that is only updated the second time final() runs.


Is there a way to avoid final being called twice?





Aucun commentaire:

Enregistrer un commentaire