ebryn / ember-model

A lightweight model library for Ember.js
MIT License
524 stars 159 forks source link

Array attr type seems to be broken #418

Closed matthewdordal closed 9 years ago

matthewdordal commented 9 years ago

When defining an Array attribute type, exactly like the documentation states, the isDirty flag on the model is not being set to true when objects are pushed (e.g. this.get('model.keywords').pushObject(keyword)).

This is the implementation in question:

App.Post = Ember.Model.extend({
  tags: attr(Array,{defaultValue:[]})
});

I debugged ember-model a bit and found Ember.attr use of Ember.computed is not recalculating for changes to properties of type Array. https://github.com/ebryn/ember-model/blob/master/packages/ember-model/lib/attr.js#L55

Is there a better practice for persisting a model property array of primitive values?

raytiley commented 9 years ago

A quick glance looks like the _dirtyAttributes get modified when the computed property is set. Pushing an object to an array does not actually replace the array, so your not calling set on the computed property.

So we probably need to add an arrayObserver to Array values. In order to set the dirtyAttributes when the array is modified.

A work around for now would be to replace the array on the model.

var keywords = this.get('model.keywords');
keywords.pushObject(keyword);
this.set('model.keywords', keywords.slice(0));

This has some performance concerns for large data sets, but it might be an acceptable workaround until this can be fixed.

ryanricard commented 9 years ago

Thanks, @raytiley. This was driving @matthewdordal and I nutty yesterday.

ebryn commented 9 years ago

@ryanricard If you want dirty checking of arrays, they have to be modeled as hasMany relationships.

ebryn commented 9 years ago

For reference, this is a duplicate of #259