Fix hasMany access within init #449

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@pivotal-medici

This commit fixes a bug in materialization when accessing a has many association within the initializer method of a record.

@wycats
Ember.js member

@eliterate is going to rebase this with a smaller patch, and then it looks good.

@pivotal-medici

Rebased.

@pivotal-medici

Any objections to this patch?

Alex Kwiatkowski, Ian Lesperance & Mark Gangl Fix hasMany association access during initialization
Accessing hasMany associations in a model's initializer was triggering a
second materialization: first when the hasMany access was attempted, then
again when the store finished initializing the new record. The second
materialization erroneously reset all of the model's data.

    App.Post = DS.Model.extend({
      comments: DS.hasMany(App.Comment),

      init: function() {
        this._super.apply(this, arguments);

        var comments = this.get('comments');

        // ...
      }
    })

This error also occurred when setting observers on a hasMany
association, as this would also trigger a hasMany access during
initialization (in order to add the array observer):

    App.Post = DS.Model.extend({
      comments: DS.hasMany(App.Comment),

      commentsDidChange: function() {
        // ...
      }.observes('comments.@each')
    })
4cc88e5
@dgeb
Ember.js member

Unfortunately, the bug that this patch fixed is still present, but we'll need to refactor a solution. DS.Model is calling this.send('materializingData'); from within materializeData() now.

@wycats @pivotal-medici - any thoughts on the right solution? This problem is also interfering with observers on hasMany arrays - see #461. I'd be glad to help out.

@dgeb dgeb referenced this pull request in emberjs/ember.js Jan 16, 2013
Closed

Issue with binding to property that depends on `init` #1789

@tomdale
Ember.js member

I think this was fixed by a recent commit.

@tomdale tomdale closed this Apr 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment