A change in an element inside a hasMany dispatches a change in the full ManyArray


#1

Hello guys,

We are updating our application to the last release of Ember Data (1.0.0-beta.12) and I’m getting an unexpected behavior when changing an item of a hasMany relationship.

With the previous versions of Ember Data (our last one was 1.0.0-beta.8), a change through the replace method would dispatch only a few observers, for each changing item, but now it is notifying a change the complete ManyArray.

Checking out the code, I found this in Ember Data:

   Model.reopen({
      notifyHasManyAdded: function(key, record, idx) {
        var relationship = this._relationships[key];
        var manyArray = relationship.manyArray;
        manyArray.addRecord(record, idx);
        //We need to notifyPropertyChange in the adding case because we need to make sure
        //we fetch the newly added record in case it is unloaded
        //TODO(Igor): Consider whether we could do this only if the record state is unloaded
        this.notifyPropertyChange(key);
      },

Is it really necessary to notify a change in the full property? I’m not sure that’s the behavior expected by most of the applications, since this wouldn’t happen for an ordinary array.

In our case, it is an issue because of our visual representation, we use Ember.computed.union to prevent views from re-rendering when swapping objects inside a collection and it turns out with this version all the views are re-created when doing it, nulling all the transition we stablished.

I created a jsfiddle to reproduce it, you can notice add an item to a hasMany relationship dispatches a change in the relationship itself.

http://jsfiddle.net/fernandodrumond/842xesgc/9/

I added an issue on github, but so far I got no answer: https://github.com/emberjs/data/issues/2555

Thanks in advance for any help!