Alex Berdyshev
Alex Berdyshev

Reputation: 761

Ember data doesn't send id when triggering PUT request

Does anybody know how can I handle this?

So, I execute the following code in my controller:

this.get('newEvent').save();

It gets my data and sends it to the server. Here's some data

Request URL:http://localhost:1337/api/v1/events/53a9cfee701b870000dc1d01

Data to send:

   event:{
      date: "Mon, 18 Aug 2014 07:00:00 GMT"
      host: "Host"
      repeatable: true
      title: "Event name"
    }

But for success I need to know the model id on my server which is normally included to the event object I send. Do you know any solutions? By the way DELETE request doesn't exclude id from the event object

Upvotes: 1

Views: 770

Answers (2)

IgorT
IgorT

Reputation: 348

You can easily override the saving behavior to include the id by overwriting the updateRecord hook in the RestAdapter: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L457 to pass includeId: true the same way createRecord does https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L436

Upvotes: 1

abuani
abuani

Reputation: 180

There are a few ways of handling this, and it comes down to personal preference. But I would recommend altering your backend code to pull the ID from the URL. Normally when you define the put request, you do it like /events/:event_id. If you did it this way on the backend, there's no need for it to be in your events object that's send over.

api/v1/events/53a9cfee701b870000dc1d01

You can capture this on the server by looking at the URL parameter that's sent over. If you're using Express on the backend, you can get the ID straight from the URL with little effort. I can't comment yet, but let me know your backend structure, and I can point you in the right direction. Possibly include the PUT request you wrote on the back end as a point of reference.

The other thing that you can do(not recommended, but will work) is to store a temporary id before doing the save.

var id = this.get('newEvent');
this.get('newEvent').set('temp_id', id).save();

And then you can access the temp_id on the server call without checking the param_id on the server call.

Upvotes: 0

Related Questions