在具有多个关系的模型上执行回滚 [英] performing rollback on model with hasMany relation

查看:107
本文介绍了在具有多个关系的模型上执行回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个组件允许删除并添加问题模型的答案。该组件随应用和取消按钮,当用户单击取消时,我想要所有更改(添加/删除答案)被还原。目前的回滚并没有做到这一点,事情在使用休息适配器的时候尝试使用了model.reload(),也没有为我工作。任何想法我可以在这种情况下做回滚?



当使用休息适配器时,我几乎都在这里指出的问题:EmberJS取消(回滚)对象与HasMany



谢谢,Dee



更新:



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

  1)从服务器获取所有答案
2)从问题
中删除答案关联3)从从服务器收到的数据手动添加答案关联

这似乎很好,但可悲的是,我得到这个错误,我不能摆脱。



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



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

  1)删除答案
2)添加答案
3)执行回滚
4)添加答案

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



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

WORK-AROUND:



一个简单的解决方法是隐藏答案删除。我修改了模型有点像:

  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')){
//新创建的答案模型没有持有DB
question.get('answers')。removeObject(answer);
answer.deleteRecord();
} else {
answer.rollback();
}
});


解决方案

我不知道你的范围,关系(我实际上在这里回滚了belongsTo,但是我很好奇,如果这有任何帮助)

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

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

我可以回滚这个约会,它的hasMany客户模型如此(从我的路线)

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


I have models defined as :

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})
});

I have a component that allows for deleting and adding answers to question model. The component comes with apply and cancel button and when the user clicks on cancel, I wanted all the changes(adds/deletes of answers) to be reverted. Currently rollback doesn't do the trick, I event tried model.reload() when using rest adapter and that didn't work for me either. Any idea how I can go about doing a rollback in this situation?

When using the rest adapter, I pretty much fall to the issue pointed here : EmberJS cancel (rollback) object with HasMany

Thanks, Dee

UPDATE :

Since I couldn't perform rollback the intended way, I performed these steps:

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

This seems to be working well BUT sadly I am getting this one error that I cannot shake off.

Here is a jsbin of updated progress: http://jsbin.com/uWUmUgE/2/

Here you can create new answer and then append it to question and do rollback too. BUT, if you follow these steps, you will see the issue I am facing:

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

It throws this error: Error: Attempted to handle event didSetProperty on while in state root.deleted.uncommitted. Called with {name: position, oldValue: 1, originalValue: 1, value: 2}.

I will super appreciate any help you can provide.

WORK-AROUND:

One simple workaround was to just hide the answers on delete. I modified the model a bit like:

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

And my rollback function had this logic:

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();
    }
});

解决方案

I'm not sure of your scope but for this relationship (I'm actually rolling back the belongsTo here but I'm curious if this helps in any way)

   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})
    });

I'm able to rollback both the appointment and it's hasMany customer model like so (from within my route)

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;
    }
}
});

这篇关于在具有多个关系的模型上执行回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆