gpt4 book ai didi

ember.js - 使用 hasMany 关系对模型执行回滚

转载 作者:行者123 更新时间:2023-12-02 13:11:59 24 4
gpt4 key购买 nike

我的模型定义为:

App.Answer = DS.Model.extend({
name: DS.attr('string'),
layoutName: DS.attr('string')
});

App.Question = DS.Model.extend({
name: DS.attr('string'),
answers: DS.hasMany('answer', {async: true})
});

我有一个组件,允许删除和添加问题模型的答案。该组件带有应用和取消按钮,当用户单击取消时,我希望恢复所有更改(添加/删除答案)。目前回滚并不能解决问题,我在使用休息适配器时尝试了 model.reload() ,但这对我来说也不起作用。知道在这种情况下如何进行回滚吗?

当使用其余适配器时,我几乎遇到了这里指出的问题:EmberJS cancel (rollback) object with HasMany

谢谢迪伊

更新:

由于我无法按预期方式执行回滚,因此我执行了以下步骤:

1) get all the answers back from the server
2) remove answer association from the question
3) manually add answer association to the question model from data received from server

这似乎运行良好,但遗憾的是我遇到了这个我无法摆脱的错误。

这里是更新进度的 jsbin:http://jsbin.com/uWUmUgE/2/

在这里您可以创建新答案,然后将其附加到问题中并进行回滚。但是,如果您按照以下步骤操作,您将看到我面临的问题:

1) delete an answer
2) add an answer
3) perform rollback
4) add an answer

它抛出这个错误:错误:尝试在 root.deleted.uncommissed 状态下处理 didSetProperty 事件。使用 {name:position,oldValue:1,originalValue:1,value:2} 进行调用。

我将非常感谢您提供的任何帮助。

解决方法:

一个简单的解决方法是隐藏删除时的答案。我对模型进行了一些修改:

App.Answer = DS.Model.extend({
name: DS.attr('string'),
layoutName: DS.attr('string'),
markToDelete: DS.attr('boolean', {default: false})
});

我的回滚函数有这样的逻辑:

answers.forEach(function (answer) {
if(!answer.get('id')){
//newly created answer models that has not persisted in DB
question.get('answers').removeObject(answer);
answer.deleteRecord();
} else {
answer.rollback();
}
});

最佳答案

我不确定你的范围,但对于这种关系(我实际上正在回滚属于这里,但我很好奇这是否有任何帮助)

   App.Appointment = DS.Model.extend({
name: DS.attr('string'),
customer: DS.belongsTo('customer', {async: true})
});

App.Customer = DS.Model.extend({
name: DS.attr('string'),
appointments: DS.hasMany('appointment', {async: true})
});

我可以回滚约会和它的 hasMany 客户模型,如下所示(从我的路线内)

App.AppointmentRoute = Ember.Route.extend({
actions: {
willTransition: function(transition) {
var context = this.get('context');
var dirty =context.get('isDirty');
var dirtyCustomer=context.get('customer.isDirty');
var message = "foo";
if ((dirty || dirtyCustomer) && confirm(message)) {
transition.abort();
}else{
context.get('customer').get('content').rollback();
context.rollback();return true;
}
}
});

关于ember.js - 使用 hasMany 关系对模型执行回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com