Permalink
Browse files

build v0.0.0

  • Loading branch information...
1 parent 3cbdfeb commit a5cbd6952559c8cb9900387b6893fb01c765361b @derickbailey derickbailey committed Sep 27, 2012
@@ -0,0 +1,86 @@
+// Backbone.EventBinder, v0.0.0
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/marionettejs/backbone.eventbinder
+(function (root, factory) {
+ if (typeof exports === 'object') {
+
+ var underscore = require('underscore');
+ var backbone = require('backbone');
+
+ module.exports = factory(underscore, backbone);
+
+ } else if (typeof define === 'function' && define.amd) {
+
+ define(['underscore', 'backbone'], factory);
+
+ }
+}(this, function (_, Backbone) {
+ "option strict";
+
+ // EventBinder
+ // -----------
+ //
+ // The event binder facilitates the binding and unbinding of events
+ // from objects that extend `Backbone.Events`. It makes
+ // unbinding events, even with anonymous callback functions,
+ // easy.
+ //
+ // Inspired by [Johnny Oshika](http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853)
+
+ Backbone.EventBinder = (function(Backbone, _){
+
+ // Constructor function
+ var EventBinder = function(){
+ this._eventBindings = [];
+ };
+
+ // Copy the `extend` function used by Backbone's classes
+ EventBinder.extend = Backbone.View.extend;
+
+ // Extend the EventBinder with additional methods
+ _.extend(EventBinder.prototype, {
+ // 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);
+
+ var binding = {
+ obj: obj,
+ eventName: eventName,
+ callback: callback,
+ context: context
+ };
+
+ this._eventBindings.push(binding);
+
+ return binding;
+ },
+
+ // 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);
+ this._eventBindings = _.reject(this._eventBindings, function(bind){return bind === binding;});
+ },
+
+ // Unbind all of the events that we have stored.
+ unbindAll: function () {
+ var that = this;
+
+ // 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);
+ });
+ }
+ });
+
+ return EventBinder;
+ })(Backbone, _);
+
+ return Backbone.EventBinder;
+
+}));
@@ -0,0 +1,5 @@
+// Backbone.EventBinder, v0.0.0
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/marionettejs/backbone.eventbinder
+(function(e,t){if(typeof exports=="object"){var n=require("underscore"),r=require("backbone");module.exports=t(n,r)}else typeof define=="function"&&define.amd&&define(["underscore","backbone"],t)})(this,function(e,t){"option strict";return t.EventBinder=function(e,t){var n=function(){this._eventBindings=[]};return n.extend=e.View.extend,t.extend(n.prototype,{bindTo:function(e,t,n,r){r=r||this,e.on(t,n,r);var i={obj:e,eventName:t,callback:n,context:r};return this._eventBindings.push(i),i},unbindFrom:function(e){e.obj.off(e.eventName,e.callback,e.context),this._eventBindings=t.reject(this._eventBindings,function(t){return t===e})},unbindAll:function(){var e=this,n=t.map(this._eventBindings,t.identity);t.each(n,function(t,n){e.unbindFrom(t)})}}),n}(t,e),t.EventBinder});
@@ -0,0 +1,66 @@
+// Backbone.EventBinder, v0.0.0
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/marionettejs/backbone.eventbinder
+// EventBinder
+// -----------
+//
+// The event binder facilitates the binding and unbinding of events
+// from objects that extend `Backbone.Events`. It makes
+// unbinding events, even with anonymous callback functions,
+// easy.
+//
+// Inspired by [Johnny Oshika](http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853)
+
+Backbone.EventBinder = (function(Backbone, _){
+
+ // Constructor function
+ var EventBinder = function(){
+ this._eventBindings = [];
+ };
+
+ // Copy the `extend` function used by Backbone's classes
+ EventBinder.extend = Backbone.View.extend;
+
+ // Extend the EventBinder with additional methods
+ _.extend(EventBinder.prototype, {
+ // 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);
+
+ var binding = {
+ obj: obj,
+ eventName: eventName,
+ callback: callback,
+ context: context
+ };
+
+ this._eventBindings.push(binding);
+
+ return binding;
+ },
+
+ // 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);
+ this._eventBindings = _.reject(this._eventBindings, function(bind){return bind === binding;});
+ },
+
+ // Unbind all of the events that we have stored.
+ unbindAll: function () {
+ var that = this;
+
+ // 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);
+ });
+ }
+ });
+
+ return EventBinder;
+})(Backbone, _);
@@ -0,0 +1,5 @@
+// Backbone.EventBinder, v0.0.0
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/marionettejs/backbone.eventbinder
+Backbone.EventBinder=function(e,t){var n=function(){this._eventBindings=[]};return n.extend=e.View.extend,t.extend(n.prototype,{bindTo:function(e,t,n,r){r=r||this,e.on(t,n,r);var i={obj:e,eventName:t,callback:n,context:r};return this._eventBindings.push(i),i},unbindFrom:function(e){e.obj.off(e.eventName,e.callback,e.context),this._eventBindings=t.reject(this._eventBindings,function(t){return t===e})},unbindAll:function(){var e=this,n=t.map(this._eventBindings,t.identity);t.each(n,function(t,n){e.unbindFrom(t)})}}),n}(Backbone,_);

0 comments on commit a5cbd69

Please sign in to comment.