[英]How to rollback changes to HasMany in ember-data
I have following models: 我有以下型号:
App.Parent = DS.Model.extend({
foo: DS.attr('string'),
children: DS.hasMany('child', {async: true})
});
App.Child = DS.Model.extend({
bar: DS.attr('string')
});
I filled them with some fixture data: 我用一些夹具数据填充了它们:
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Parent.FIXTURES = [
{
id:0,
foo: 'parent',
children: [0,1]
}
];
App.Child.FIXTURES = [
{
id: 0,
bar: 'child 0'
},
{
id: 1,
bar: 'child 1'
},
{
id: 2,
bar: 'child 2'
}
];
After I do some changes to the children
relation, how can I rollback children
relation to its latest saved state? 我做了一些改动后children
的关系,我怎么可以回滚children
关系到其最新的保存的状态?
I push new child to manyArray in the following way: 我通过以下方式将新子项推入manyArray:
this.store.find('child', 2).then(function(child){
model.get('children').pushObject(child);
})
This does change the relation (I see new child in my view), but parent record does not become dirty. 这确实改变了关系(我在视图中看到一个新的孩子),但是父记录不会变脏。 Thus, when I try model.rollback()
it does nothing. 因此,当我尝试model.rollback()
它什么也不做。 I also tried solution I found here How to rollback relationship changes in EmberData which is adding model.send('becomeDirty')
before rollback, but it does not help. 我还尝试了在这里找到的解决方案,该方法如何回滚EmberData中的关系更改,该更改在回滚之前添加了model.send('becomeDirty')
,但这无济于事。
Maybe I am adding children to my relation in a wrong way? 也许我以错误的方式将孩子添加到我的关系中?
Thanks! 谢谢!
I used this to rollback related records that are dirty. 我用它来回滚脏的相关记录。
App.ParentRoute = Ember.Route.extend
model: ->
@get('store').createRecord('parent', child: @get('store').createRecord('child'))
actions:
willTransition: (transition) ->
rollbackRecords(@)
rollbackRecords = (context) ->
if context.get("controller.content.isDirty")
relationships = Ember.get(App[context.get('controller').resourceName], "relationshipsByName")
content = context.get('controller.content')
relationships.forEach (name, relationship) ->
relatedModel = content.get(name)
relatedModel.rollback() if relatedModel? and relatedModel.get('isDirty')
content.rollback()
true
Here is some code that will rollback a model, as well as it's relationships that I use: 这是一些将回滚模型的代码以及我使用的关系:
var model = this.get('model');
var relatedModel, relatedModels;
model.constructor.eachRelationship(function (key, relationship) {
if (relationship.kind === 'hasMany') {
relatedModels = model.get(key);
if (relatedModels) {
relatedModels.invoke('rollback'); //since this is an array, need to call the rollback function differently
}
}
else {
relatedModel = model.get(key);
if (relatedModel) {
relatedModel.rollback();
}
}
});
model.rollback();
Hope this helps 希望这可以帮助
I believe the other answers listed here only partially solve this problem. 我相信这里列出的其他答案只能部分解决此问题。 If you add a new related model or remove an existing one, rollback should reverse those as well and I believe the other answers don't address this. 如果添加新的相关模型或删除现有模型,则回滚也应逆转这些模型,我相信其他答案不能解决此问题。 Here is a complete solution that provides proper dirty checking and a complete rollback for hasMany and belongsTo relationships: 这是一个完整的解决方案,为hasMany和belongsTo关系提供正确的脏检查和完整的回滚:
https://stackoverflow.com/a/27184207/188740 https://stackoverflow.com/a/27184207/188740
我喜欢做一个很好的model.reload()
-它会吹走所有尚未与服务器同步的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.