stravid
stravid

Reputation: 1009

DS.Model callback which is invoked after a record is created and the server-side id is set

When I create a new DS.Model record and call commit on the store I would like to know when the record was created in the persistence layer and I have access to the id attribute.

I thought the didCreate callback would do exactly that. But to my surprise the id is still undefined when the didCreate gets invoked.

So my question is basically, am I doing something wrong and there is a better callback for my use-case or is this a bug?

Upvotes: 3

Views: 1012

Answers (1)

pangratz
pangratz

Reputation: 16143

Hmm, it looks like this test case should cover this. I think you should file a ticket.

As a workaround you could use something along those lines, see http://jsfiddle.net/pangratz666/ZkQHE/:

App.IdWatcher = Ember.Mixin.create({
    init: function() {
        this._super();
        this.addObserver('data', this, '_dataDidChange');
    },
    _dataDidChange: function() {
        var id = this.get('id');
        if (id) {
            this.idHasBeenDefined(id);
            this.removeObserver('data', this, '_dataDidChange');
        }
    }
});

App.Model = DS.Model.extend(App.IdWatcher, {
    label: DS.attr('string'),

    idHasBeenDefined: function(id) {
        console.log('id is set to %@'.fmt(id));
    }
});

Upvotes: 2

Related Questions