server updates when using Ember Data RestAdapter

Using Ember-Data with the RestAdapter, I'm able to successfully get data from my server. I display this data using Handlebar's {{input}} helper. The use case is relatively simple: I want to allow users to update the data, and have the modifications persisted back to the server.

Here are my questions:

1) When I modify the data shown on the screen, does the Ember-Data "store" get updated behind the scene automatically as soon as I tab out of a particular field?

2) In monitoring server incoming messages, I don't see an http "PUT" request from Ember when I tab out of a field, or when I change route. So how do I go about triggering an update to the server?

follow-up comments

Per Amir's answer below, I got the RestAdapter to send a "PUT" request to the server by using "this.get('model').save();". But RestAdapter didn't send the hasMany records. So if my model definition is as shown below (please note that both 'item' and 'strat' have IDs, which are not shown in the model definition):

App.Item = DS.Model.extend({
  itemName: DS.attr('string'),
  strategy: DS.belongsTo('strat')
});

App.Strat = DS.Model.extend({
    stratName: DS.attr('string'),
    items: DS.hasMany('item',{async:true})
});

"this.get('model').save()" triggered a "PUT" request to the server, but the JSON sent only contained stratName. How do I get RestAdapter to send the hasMany record?


You decide when you want to persist the object (to the server) by calling save on the model (probably from a controller).

Note that ember-data isn't production ready at this point but for your simple case it should be fine :-)

Here is a fiddle that shows saving a new record. In the chrome inspector you can see it doing a GET and a POST. If you had a record already loaded and hit save, it would do a PUT.

Saving hasMany

When the RESTAdapter serializes the object to JSON it goes through all of the properties. If the properties are relationships they might not be fulfilled yet and might not be sent to the backend. (Or are sent to the backend as undefined );

When I need a relationship to be saved (especially if it is async) I have been wrapping the save in a get to ensure the data is ready.

strat.get('items').then(function(){
  strat.save(); //at this point items is populated
})

I'm sure there is a better way to do this and would be happy to learn it!


Option 1

Serializer, with DS.EmbeddedRecordsMixin:

attrs: {
    // if the kids are embedded to and from server:
    childAttribute: { embed: 'always' },
    // if sending the kids with data to the server:
    childAttTwo: { serialize: 'records', deserialize: false }
}

Option 2

Alternative is via an addon Ember-CLI Stained By Children which is a collection of Mixins which addresses this issue with Child Model data. I also adore the nomenclature: stained-by-children and clean-embedded-children! So appropriate for this dilemma!

链接地址: http://www.djcxy.com/p/90954.html

上一篇: REST风格的JSON API可以与Ember一起使用

下一篇: 服务器更新时使用Ember Data RestAdapter