How can I cancel a model belongs to a model

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: ------------------------------- 

      

I need to simulate like this:

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

      

then i use select input in template:

{{#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}}

      

and my controller looks like this:

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

      

The problem seems to indicate that the ember data cannot successfully undo the matched model, and even it cannot flag the dirty model property exactly when the belongsTo property changed.

My question is, how can I code the rollback problem with the car owner? For the model as above.

+3


source to share


2 answers


I have searched for the problem on our site, but I cannot find a solution, one is just like ' How to change the rollback relationship in EmberData ', in this case it has a hasMany relationship, but in my case there is only belongsTo attribute.



0


source


Late to the party, but here we go:

I have created an addon that solves this problem. Just call rollbackAttributes()

and it will rollback your relationship as well:



https://www.npmjs.com/package/ember-rollback-relationships

0


source







All Articles