VB_
VB_

Reputation: 45702

X-Editable & Backbone model previousAttributes()

What I have:

I have x-editable popup which should check input parameters with help of validate method

$(".vacancy-edit-select-status").editable({
    value: $this.model.get('value').id,
    source: source,
    validate: function(value) {
        $this.model.previousAttributes(); //UNDEFINED!!!
        //....            
    },
    success: function (response, newValue) {
        $this.model.set('value', newValue);
    }
});

What a problem:

Inside validate method previousAttributes() method return 'undefined'. (I've changed some model attributes before change x-editable popup's variables, so the history shouldn't be empty)

Question:

How can I access model history from validate method?

Upvotes: 0

Views: 455

Answers (2)

VB_
VB_

Reputation: 45702

The answer is simple and maybe not clear enough from my question:

Backbone clones model state only on change event.

Moreover, when you have a collection as an model attribute there are also some nuances with previousAttributes. For example, to write attribute change to the history you have to rewrite the whole attribute. I mean

this.model.get('myCommentsCollection').push('newComment'); //DOESN'T WRITE ANYTHING TO previousAttributes

So to write something to model.previousAttributes you should do:

var comments = _.clone(this.model.get("comments"));
this.model.set('myCommentsCollection', comments);

Upvotes: 0

krasu
krasu

Reputation: 2037

previousAttributes populating during set, validate calling before population of previousAttributes - reference

Upvotes: 1

Related Questions