Adam Krawesky
Adam Krawesky

Reputation: 1353

in ember-data, when in the lifecycle is a newly created record's id available?

using the RESTAdapter in ember-data. i create a record on the server, and i need to get the id of the newly created record. that id gets passed back from the server, i can see it in the json returned.

however, in the DS.Model's didCreate() callback, this.get('id') is undefined.

i also tried adding a record.one('didCreate',aClass,aClass.onDidCreate) and checking newRecord.get('id') but it's also undefined.

the didCommit callback doesn't seem to get called after createRecord() , so i'm not sure where else i should be checking in the lifecycle callbacks. anyone know?

Upvotes: 3

Views: 1249

Answers (1)

mehulkar
mehulkar

Reputation: 4964

I don't know what the hook is for the returned id, but I've been setting an observer on 'id' to fire a method like so:

record.addObserver('id', this, 'actionToFire');

That seems to do the trick.

Update: Remember to removeObserver in actionToFire though so they don't pile up!

Upvotes: 2

Related Questions