-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change event firing too soon in set function #16
Comments
@jpchip I think I got a fix for your issue on master. As you stated, the model was being bound twice. To fix this, before the given model is re-bound, I unstick any bindings. I have never re-rendered after binding, but I can see why it may be needed, and stickit shouldn't bug-out when it happens. I created the following fiddles which render twice, look at the internal _modelBindings array, and display the number of bindings (should be 2): http://jsfiddle.net/px6UP/3/ (fix in master; should show 2 model bindings) Let me know if this fixes your issue, or if I just squashed another issue, so I can cut a 0.5.3 tag ASAP. |
I tested your change, and it did not fix my issue. I still needed to put the "ret = oldSet.call(this, attrs, options);" call after the bind triggering. I might just be doing something wrong in my view. I will see if I can set up a fiddle showing the error occurring. I guess you squashed another bug though, so that's good! Thanks for looking into the issue! |
@jpchip I setup the following fiddle which re-renders after every change in the model: My fix on master definitely fixes the model being re-bound every time render fires. I'm going to close this issue for now. Feel free to reopen if you can replicate or better describe your issue. Also, don't hesitate to ask questions. |
This might be a problem just for me, but I thought I'd report it anyways.
In the wrapping of the set function, the Backbone.model.set() call comes before the attributes are iterated though and have a bind event triggered on each of them:
// Delegating to Backbone's model.set().
ret = oldSet.call(this, attrs, options);
// Iterate through the attributes that were just set.
.each(.keys(attrs || {}), .bind(function(attr) {
// Trigger a custom "bind" event for each attribute that has changed, unless {bind:false} option.
if (( !.isEqual(now[attr], val) || (options.unset && _.has(now, attr))))
this.trigger('bind:' + attr, attrs[attr], options);
}, this));
If the view is listening to a change event on the model to render, and the render function calls this.stickit(), the bindings end up being applied twice. Moving the call to the Backbone.model.set() to after the bind events are triggered seems to fix this:
// Iterate through the attributes that were just set.
.each(.keys(attrs || {}), .bind(function(attr) {
// Trigger a custom "bind" event for each attribute that has changed, unless {bind:false} option.
if (( !.isEqual(now[attr], val) || (options.unset && _.has(now, attr))))
this.trigger('bind:' + attr, attrs[attr], options);
}, this));
// Delegating to Backbone's model.set().
ret = oldSet.call(this, attrs, options);
The text was updated successfully, but these errors were encountered: