admin管理员组文章数量:1406149
This may be a result of misuse of the ponent, though I don't think so. I have an issue where a View updates a model in Backbone JS and calls the model's Set method so that it may verify it's input. In theory there are two results to such an action: Error and Change. Both events work as prescribed. But in fact there is a third event: No change. That is, if the input has not been changed at all, I can't tell after calling Set because no error will be thrown but nor will a change event, as nothing has actually changed- but I still want to know about such a case. Is there a way for me to do this?
The reason is that there is an action I want performed only if no error occurs, but there is no way for me to know (without a change event) that the model has attempted to set the new values and ended with no result as it all happens asynchronously.
Thanks!
This may be a result of misuse of the ponent, though I don't think so. I have an issue where a View updates a model in Backbone JS and calls the model's Set method so that it may verify it's input. In theory there are two results to such an action: Error and Change. Both events work as prescribed. But in fact there is a third event: No change. That is, if the input has not been changed at all, I can't tell after calling Set because no error will be thrown but nor will a change event, as nothing has actually changed- but I still want to know about such a case. Is there a way for me to do this?
The reason is that there is an action I want performed only if no error occurs, but there is no way for me to know (without a change event) that the model has attempted to set the new values and ended with no result as it all happens asynchronously.
Thanks!
Share Improve this question edited Jan 8, 2012 at 22:34 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Nov 20, 2011 at 20:51 Che KofifChe Kofif 8611 gold badge9 silver badges22 bronze badges2 Answers
Reset to default 8Every Backbone model has a hasChanged
method:
hasChanged
model.hasChanged([attribute])
Has the model changed since the last
"change"
event? If an attribute is passed, returnstrue
if that specific attribute has changed.
Perhaps you can use that to check your third possibility.
BTW, the callbacks aren't asynchronous. The error and changed callbacks are triggered and return before set
returns:
set : function(attrs, options) {
//...
// Run validation.
if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
//...
// Update attributes.
for (var attr in attrs) {
var val = attrs[attr];
if (!_.isEqual(now[attr], val)) {
now[attr] = val;
delete escaped[attr];
this._changed = true;
if (!options.silent) this.trigger('change:' + attr, this, val, options);
}
}
The _performValidation
call triggers the error callbacks, the this.trigger
calls will call the per-attribute callbacks.
In this case, you may need to dance around Model.set()
a little bit to get where you want. If you are using this functionality, then you should have defined a validate()
method on your model.
http://documentcloud.github./backbone/#Model-validate
So you can call this method directly...
// something happens and we need to update the model to "newvalues"
if (model.validate(newvalues)) {
model.trigger('error')
} else {
model.trigger('change')
}
model.set(newvalues)
That way you will always at least get 'change' or 'error' out of it, even if it's the same. You will also still get the existing events from set.
本文标签: javascriptHow can I tell if a model has not been changed in BackbonejsStack Overflow
版权声明:本文标题:javascript - How can I tell if a model has not been changed in Backbone.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744118436a2591622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论