I’m trying to get a View in Backbone.js to save when there’s a ‘change’ event if (and only if) the data has changed.
Long story short, I’ve a ‘change’ event set on the View that calls this:
function changed_event() {
log.debug("Before: " + this.model.get('name')) // not 'contrived!'
this.model.set({'name': 'contrived!'});
log.debug("After: " + this.model.get('name')) // 'contrived!'
if (this.model.hasChanged()) {
alert("This is never called.");
}
}
I’d like to know why the model.hasChanged() is false when clearly the model has been changed.
I’m not sure what other information is necessary, but if there is more information that may be helpful please comment and I’ll elaborate.
Thank you for reading.
By calling
model.set, you are firing a change event..hasChanged()returns true iff the model has changed since the last change event. So it will return false since nothing changed since that change event. To get the behaviour you want, call.setwith thesilentoption:this.model.set({'name': 'contrived!'}, {silent: true})