This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

build v0.1.0

  • Loading branch information...
derickbailey committed Oct 18, 2012
1 parent b2186eb commit 60ca372fa1b20f9898640cf2c2ce20fd190c2e22
Showing with 146 additions and 42 deletions.
  1. +71 −19 lib/amd/backbone.eventbinder.js
  2. +2 −2 lib/amd/backbone.eventbinder.min.js
  3. +71 −19 lib/backbone.eventbinder.js
  4. +2 −2 lib/backbone.eventbinder.min.js
@@ -1,4 +1,4 @@
-// Backbone.EventBinder, v0.0.0
+// Backbone.EventBinder, v0.1.0
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
// http://github.com/marionettejs/backbone.eventbinder
@@ -29,6 +29,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(){
@@ -40,18 +99,15 @@
// 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);
@@ -60,21 +116,17 @@
// 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);
}
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -1,4 +1,4 @@
-// Backbone.EventBinder, v0.0.0
+// Backbone.EventBinder, v0.1.0
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
// http://github.com/marionettejs/backbone.eventbinder
@@ -13,6 +13,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 +83,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 +100,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);
}
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 60ca372

Please sign in to comment.