amique
amique

Reputation: 2236

Ember data : CreateRecord and transition

I am using ember-data 1.0.0-beta.3 RestAdapter. After creating model, I transition to that newly created record but the Id is being shown as 'null' in URL.

What JSON should I return from my POST service.

Here is the code from, action of my route, to creating record.

  var user = this.store.createRecord('user', model);
  user.save();
  this.transitionTo('user', user);

The REST call is successful i.e. record is created in DB but id is show as null in URL i.e. http://myserver.com/#/user/null

I return following JSON

{"user": [{"id":"15","name":"su"}]}

I have also tried to return only id i.e. "15" but it has no effect.

Please guide?

Upvotes: 0

Views: 613

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

Have you tried:

var user = this.store.createRecord('user', model);
this.transitionTo('user', user.save());

transitionTo accept a promise as second argument, and will wait it be finished, and get the resolved data. So ensures that you have the id.

And maybe you need to change your payload to:

{"user": {"id":"15","name":"su"} }

Since you're saving a single resource, not an array.

Upvotes: 1

Related Questions