Permalink
Browse files

updated eventbinder for tests and bundle

  • Loading branch information...
1 parent 198b620 commit c05ccd9d22ba885478f61ce0ae7c6130fe3958b1 @jsoverson jsoverson committed Oct 10, 2012
Showing with 70 additions and 22 deletions.
  1. +70 −22 public/javascripts/backbone.eventbinder.js
@@ -1,7 +1,3 @@
-// Backbone.EventBinder, v0.0.0
-// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
-// Distributed under MIT license
-// http://github.com/marionettejs/backbone.eventbinder
// EventBinder
// -----------
//
@@ -13,6 +9,65 @@
// Inspired by [Johnny Oshika](http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853)
Backbone.EventBinder = (function(Backbone, _){
+ "use strict";
+
+ // A map of objects that support binding/unbinding events.
+ // This allows EventBinder to support events on arbitrary
+ // objects with EB's consistent api.
+ var handlerMap = {
+ // 'default' type accounts for Backbone style objects extending
+ // Backbone.Events
+ default : {
+ bindTo : function (obj, eventName, callback, context) {
+ context = context || this;
+ obj.on(eventName, callback, context);
+
+ var binding = {
+ type : 'default',
+ obj: obj,
+ eventName: eventName,
+ callback: callback,
+ context: context
+ };
+
+ return binding;
+ },
+ unbindFrom : function(binding){
+ binding.obj.off(binding.eventName, binding.callback, binding.context);
+ }
+ },
+
+ // 'jquery' style handlers allow us to bind to jQuery
+ // (or compatible) objects
+ jquery : {
+ bindTo : function (obj, eventName, callback, context) {
+ context = context || this;
+ callback = _(callback).bind(context);
+ obj.on(eventName, callback);
+
+ var binding = {
+ type : 'jquery',
+ obj: obj,
+ eventName: eventName,
+ callback: callback,
+ context: context
+ };
+
+ return binding;
+ },
+ unbindFrom : function(binding){
+ binding.obj.off(binding.eventName, binding.callback);
+ }
+ }
+ };
+
+ // Use whatever best logic necessary to determine the type
+ // of the supplied object
+ function getHandlerForObject(obj) {
+ if (obj.jquery) return handlerMap.jquery;
+
+ return handlerMap.default;
+ }
// Constructor function
var EventBinder = function(){
@@ -24,18 +79,15 @@ Backbone.EventBinder = (function(Backbone, _){
// Extend the EventBinder with additional methods
_.extend(EventBinder.prototype, {
- // Store the event binding in array so it can be unbound
+
+ // Delegate to the bindTo for the appropriate type and
+ // store the event binding in array so it can be unbound
// easily, at a later point in time.
- bindTo: function (obj, eventName, callback, context) {
- context = context || this;
- obj.on(eventName, callback, context);
+ bindTo: function(/* args... */) {
+ var obj = arguments[0];
+ var handlers = getHandlerForObject(obj);
- var binding = {
- obj: obj,
- eventName: eventName,
- callback: callback,
- context: context
- };
+ var binding = handlers.bindTo.apply(this,arguments);
this._eventBindings.push(binding);
@@ -44,21 +96,17 @@ Backbone.EventBinder = (function(Backbone, _){
// Unbind from a single binding object. Binding objects are
// returned from the `bindTo` method call.
- unbindFrom: function(binding){
- binding.obj.off(binding.eventName, binding.callback, binding.context);
+ unbindFrom: function(binding) {
+ handlerMap[binding.type].unbindFrom.apply(this,arguments);
this._eventBindings = _.reject(this._eventBindings, function(bind){return bind === binding;});
},
// Unbind all of the events that we have stored.
- unbindAll: function () {
- var that = this;
-
+ unbindAll: function() {
// The `unbindFrom` call removes elements from the array
// while it is being iterated, so clone it first.
var bindings = _.map(this._eventBindings, _.identity);
- _.each(bindings, function (binding, index) {
- that.unbindFrom(binding);
- });
+ _.each(bindings, this.unbindFrom, this);
}
});

0 comments on commit c05ccd9

Please sign in to comment.