如何让Ember-Data RESTAdapter发送“hasMany”记录到服务器 [英] how to get Ember-Data RESTAdapter to send "hasMany" records to server
问题描述
我使用的是Ember-Data beta 3.我的Ds.models类似于以下内容:
I'm using Ember-Data beta 3. My Ds.models are similar to the following:
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})
});
使用Ember-data的RESTAdapter,我可以使用我的服务器中的数据填充模型。但是当我尝试将数据保留回服务器时,没有发送App.Item记录。我的服务器收到的JSON只包含stratName。我使用this.get('model')。save()来触发发送。
Using Ember-data's RESTAdapter, I'm able to populate the model using data from my server. But when I tried to persist data back to the server, none of the "App.Item" records got sent. The JSON received by my server only contained "stratName." I used "this.get('model').save()" to trigger the send.
我做错了什么?
推荐答案
有没有原因,您在使用模型之间的异步关系?
Is there a reason why you are using an async relationship between your models?
他们作为{embedded:always},您可以轻松地覆盖serializeHasMany函数以包含所有相关的模型信息
If you set them as {embedded: always}, you can easily override the serializeHasMany function to include all of your related model information
serializeHasMany: function(record, json, relationship) {
var hasManyRecords, key;
key = relationship.key;
hasManyRecords = Ember.get(record, key);
if (hasManyRecords && relationship.options.embedded === "always") {
json[key] = [];
hasManyRecords.forEach(function(item, index) {
// use includeId: true if you want the id of each model on the hasMany relationship
json[key].push(item.serialize({ includeId: true }));
});
} else {
this._super(record, json, relationship);
}
},
这篇关于如何让Ember-Data RESTAdapter发送“hasMany”记录到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!