如何在Ember Data中保存关联模型的更改 [英] How to save changes on associated models in Ember Data
问题描述
我有这些数据:
{
product:{
id:185588,
name:USB MIDI Adapter,
image:[imageid1,imageid2....]
},
图像:[
{
id:imageid1,
url:google.com
},
{
id:imageid2,
url:google.com2
}
]
}
这些模型:
App.Image = DS.Model.extend({
url:DS.attr('string')
} );
App.Product = DS.Model.extend({
name:DS.attr('string'),
images:DS.hasMany('Image')
});
在我的模板中,我有一个编辑窗体来更改相关图像上的URL:
< form {{action save on =submit}}>
{{input type =textvalue = name}}
{{#each images}}
* {{input type =textvalue = url}} <峰; br>
{{/ each}}
< button type =submit> Save< / button>
< / form>
保存操作是:
save:function(){
var product = this.modelFor('productEdit');
product.save();
this.transitionTo('product',product);
}
问题是保存操作只会保存产品(PUT产品)但不是对图像url所做的更改。模型绑定工作正常,当我更改图像上的URL时,gui将按原样更新,但PUT不会按原样发送。
如何保存图像变化?
您需要在每个记录上调用save,在recordarray上调用save(将重复并保存),或者您可以覆盖序列化程序( serializeIntoHash
)将图像注入产品的保存。
records.forEach(function(record){
if(record.get('isDirty'))record.save();
});
I'm running Ember Data 1.0.0 with Ember 1.3.0.
I have this data:
{
"product": {
"id": 185588,
"name": "USB MIDI Adapter",
"images":["imageid1", "imageid2"....]
},
"images": [
{
"id": "imageid1",
"url": "google.com"
},
{
"id": "imageid2",
"url": "google.com2"
}
]
}
And these models:
App.Image = DS.Model.extend({
url: DS.attr('string')
});
App.Product = DS.Model.extend({
name: DS.attr('string'),
images: DS.hasMany('Image')
});
In my template I have an edit form to change the url on the associated images:
<form {{action save on="submit"}}>
{{input type="text" value=name}}
{{#each images}}
* {{input type="text" value=url}}<br>
{{/each}}
<button type="submit">Save</button>
</form>
The save action is:
save: function() {
var product = this.modelFor('productEdit');
product.save();
this.transitionTo('product', product);
}
The problem is that the save action only saves the product (PUT product), but not the changed that's made to the image urls. The model binding works fine, when I change the url on images the gui is updated as it should, but the PUT is not sent as it should.
How do I save the image changes?
You either need to call save on each record, call save on the recordarray(which will iterate and save), or you can override the serializer (serializeIntoHash
) to inject the images into the save of the product.
records.forEach(function(record){
if(record.get('isDirty')) record.save();
});
这篇关于如何在Ember Data中保存关联模型的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!