我如何回滚belongsTo模型

时间:2014-12-30 00:40:14

标签: ember.js ember-data rollback

DEBUG: ------------------------------- ember-1.9.1.js:3935
DEBUG: Ember      : 1.9.1 ember-1.9.1.js:3935
DEBUG: Ember Data : <%= versionStamp %> ember-1.9.1.js:3935
DEBUG: Handlebars : 2.0.0 ember-1.9.1.js:3935
DEBUG: jQuery     : 1.11.1 ember-1.9.1.js:3935
DEBUG: ------------------------------- 

我必须像这样建模:

Hwv.Car = DS.Model.extend({
    number: DS.attr('string'),
    owner: DS.belongsTo('user')
});
Hwv.User = DS.Model.extend({
    name: DS.attr('string'),
    phone: DS.attr('string'),
    email: DS.attr('string')
});

然后我在模板中使用选择输入:

{{#if isEditing}}
  {{view "select" id = "owner" class="form-control"
    content=owners
    optionLabelPath="content.name"
    optionValuePath="content.id"
    prompt="--please select a user--"
    selection=selectedOwner
  }}
  <span class="glyphicon form-control-feedback"></span>
{{else}}
  <p class="form-control-static">{{owner.name}}</p>
{{/if}}

我的控制器是这样的:

Hwv.CarController = Ember.ObjectController.extend({
    needs:["application","session"],
    isEditing:false,
    isNew:false,
    owners:function(){
        var model = this.get('model'),
            store = this.store;
        return store.filter('user', function (user) {
            return true;
        });
    }.property(),
    selectedOwner:function(k,v){
        var model = this.get('model');
        if(!model){
            return;
        }
        if (v === undefined) {
            return model.get("owner");
        } else {
            debugger;
            model.set('owner', v);
            debugger;
            return v;
        }
    }.property("model.owner"),
    selectedOwnerDidChange: function() {
        debugger;
        //if i remove this row ,the car model can't be dirty when the owner of the car is changed by the select input.
        this.get('model').send('becomeDirty');
    }.observes('selectedOwner'),
    actions:{
        cancel:function(){
            //this row will work when i change the car number only and then click the cancel button
            //but when i change the car owner by the select input,the car model can't rollback successfully.
            this.get("model").rollback();
        }
    }
});

问题似乎指出,ember数据无法成功回滚belongsTo模型, 甚至当belongsTo属性发生变化时,它也无法准确标记模型的脏属性。

我的问题:我如何编码以解决与车主belongsTo模型的回滚问题,如上所述。

0 个答案:

没有答案