Getting rendered json from ember.save () method
My backend rails render a json response when I call .save () on my model. When doing a jQuery ajax PUT request like this, I can get the returned data (see the data parameter below):
$.ajax({
type : 'PUT',
url: '/conversations/' + conversation.id + '/assignation',
dataType: 'json',
data: newAssignation,
success: function(data) {
conversation.get('content').pushObject(data)};
});
How can I get the processed json when the .save () method is called in my model?
self.store.find('conversation', conv.id).then(function(conversation){
conversation.set('status', 'opened');
conversation.save(); ** This returns json response that I want to retrieve **
});
source to share
When you execute record.save()
, the following things happen (as of Ember Data 1.0.0-beta-19.1):
- You run
record.save()
. - Record informs the store to schedule the save operation:
store.scheduleSave()
. - Once per cycle runloop store resets scheduled save operation:
store.flushPendingSave()
. - Store requests recording adapter to perform the backup:
adapter.updateRecord()
. - The adapter asks the record serializer to convert the record to a JSON: payload
serializer.serializeIntoHash
. - The adapter builds the URL:
adapter.buildURL
. - The adapter makes a request to the server.
- The store asks for the serializer to convert the response from the server to data and insert it into the store:
serializer.extract()
βserializer.extractSave()
βserializer.extractSingle()
. - The serializer runs
serializer.normalizePayload()
, which does nothing except you can override it to clean up the payload. - The serializer prepares the payload and pushes the payload into the store as a result.
- The store executes his method
store.didSaveRecord()
. - The store fires an event
didCommit
on the model. - Finally, the promise returned
record.save()
in paragraph 1 resolves.
Some steps are missing from this list, I've only highlighted the important ones. I urge you to follow the entire procedure in the sources, I have given you enough links.
Now to your question. I suggest you override the method serializer.extractSave()
. There you can convert your payload to Ember format and play with it. Remember to call this.super()
with all the arguments and the modified payload so that Ember will actually push it back into the store!
source to share