Browse files

fixed how the event binder is attached to views. bind 'render' to the…

… view instance, always
  • Loading branch information...
1 parent 26eea4c commit 722e79c6e2bb9c1872e498b76ae933ac5d320496 @derickbailey derickbailey committed Oct 6, 2012
Showing with 19 additions and 3 deletions.
  1. +2 −0 changelog.md
  2. +1 −1 spec/javascripts/collectionView.spec.js
  3. +16 −2 src/marionette.view.js
View
2 changelog.md
@@ -9,8 +9,10 @@
* All Views
* **BREAKING:** `beforeRender` method is now `onBeforeRender`
* **BREAKING:** `beforeClose` method is now `onBeforeClose`
+ * **BREAKING:** The `render` method for all Marionette views is bound to the view instance
* All view events are now triggered with `triggerMethod`, calling their corresponding method on the view if it exists
* All views now have an `isClosed` attribute on them, which is set to `true` when calling the `close()` method and reset to `false` when calling the `render()` method
+ * EventBinder is now attached to the views as an `eventBinder` attribute, with methods from the event binder attached to the view directly
* CompositeView
* **BREAKING:** CompositeView will only render a model in to it's template, instead of a model or collection. It will still render the collection as itemView instances.
View
2 spec/javascripts/collectionView.spec.js
@@ -524,7 +524,7 @@ describe("collection view", function(){
});
it("should not retain any bindings to its children", function(){
- expect(_.size(collectionView._eventBindings)).toBe(0);
+ expect(_.size(collectionView.eventBinder._eventBindings)).toBe(0);
});
it("should unbind any listener to custom view events", function(){
View
18 src/marionette.view.js
@@ -5,8 +5,8 @@
Marionette.View = Backbone.View.extend({
constructor: function(){
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder);
+ _.bindAll(this, "render");
+ this._configureEventBinder();
Backbone.View.prototype.constructor.apply(this, arguments);
@@ -20,6 +20,20 @@ Marionette.View = Backbone.View.extend({
// methods if the method exists
triggerMethod: Marionette.triggerMethod,
+ // Internal method. Add the EventBinder methods to the view directly,
+ // but keep them bound to the EventBinder instance so they work properly.
+ // This allows the event binder's implementation to vary independently
+ // of it being attached to the view... for example the internal structure
+ // used to store the events can change without worry about it interfering
+ // with Marionette's views.
+ _configureEventBinder: function(){
+ var eventBinder = new Marionette.EventBinder();
+ this.eventBinder = eventBinder;
+ this.bindTo = _.bind(eventBinder.bindTo, eventBinder);
+ this.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
+ this.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
+ },
+
// Get the template for this view
// instance. You can set a `template` attribute in the view
// definition or pass a `template: "whatever"` parameter in

0 comments on commit 722e79c

Please sign in to comment.