4
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 have to model just 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 a select input in the 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 is 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 seem to point out the ember data can't rollback a belongsTo model successfully, and even it can't mark the dirty property of the model exactly when a belongsTo property has changed.

My ask: How can i code to fix the rollback issue with the car owner belongsTo model just like above.

lit_yin
  • 43
  • 2

0 Answers0