Jm add compositeview bindto #7

Merged
merged 4 commits into from Sep 28, 2012

1 participant

@jasonm
  • Fixes SwappingRouter spec
  • Adds CompositeView#bindTo, #unbindFromAll. Invokes #unbindFromAll from within #leave.
@jferris jferris and 1 other commented on an outdated diff Sep 28, 2012
...assets/javascripts/backbone-support/composite_view.js
this.remove();
this._leaveChildren();
this._removeFromParent();
},
+ bindTo: function(source, event, callback) {
+ source.bind(event, callback, this);
+ this.bindings = this.bindings || [];
+ this.bindings.push({ source: source, event: event, callback: callback });
+ },
+
+ unbindFromAll: function() {
+ _.each(this.bindings, function(binding) {
@jferris
thoughtbot, inc. member
jferris added a note Sep 28, 2012

Since bindings isn't initialized unless bindTo is invoked, will this cause errors if the view is removed without ever binding to anything?

@jferris
thoughtbot, inc. member
jferris added a note Sep 28, 2012

It looks like we initialize children in the prototype constructor, so you could initialize bindings there, too.

@jasonm
jasonm added a note Sep 28, 2012

I tried to motivate with a failing test, but it turns out _.each(undefined, function(){}); passes silently. I agree that explicitly initializing bindings in the constructor is more clear anyhow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jasonm jasonm merged commit ea5ea57 into master Sep 28, 2012
@ecbypi ecbypi deleted the jm-add-compositeview-bindto branch Feb 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment