dimanche 6 septembre 2015

Handle JSON Response After save() on a Model

In an application using the standard Ember 2.0 DS.RESTAdapter, the default behaviour for the REST API responding to a save() is "to return an empty object ({})."[1]

In my case, my REST API performs some changes to the data—including assigning a bigserial int64 ID from Postgres and processing text entry to Markdown (much, much faster than Ember can). This should (has to be) synchronous/blocking.

Is there an idiomatic approach to overriding save() on the default 2.0 REST adapter so that the returned JSON model is stored in Ember's cache before the promise is called - e.g. user.save().then(transitionToUser).catch(failure);?

Should I be defining my own save() method - and if so, where/how?

// app/models/user.js
export default DS.Model.extend({
  id: DS.attr("number"),
  name: DS.attr(),
  email: DS.attr()
});

function save(?) {
  // What goes here?
}




Aucun commentaire:

Enregistrer un commentaire