Browse files

added Marionette.addEventBinder method to properly add an instance of…

… the EventBinder object to a specified target object, and updated all code to use this method call
  • Loading branch information...
1 parent 4027d50 commit 8bc76afda7be3a847afff5e6a59d4f98a01e9f8d @derickbailey derickbailey committed Oct 6, 2012
View
13 changelog.md
@@ -12,11 +12,14 @@
* **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
+ * EventBinder is now attached to the views with the `Marionette.addEventBinder` method call
* 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.
+* Modules
+ * **BREAKING:** Split module definitions can now receive custom args per module definition, instead of sharing / replacing them across all definitions
+
* CollectionView / CompositeView
* Cleaned up the `getItemViewContainer` code, and improved the error that is thrown when the specified container element is not found
* Can attach existing view instance w/ existing DOM element as child of collection view / composite view, in parent's `initialize` function
@@ -28,19 +31,19 @@
* View / ItemView / CompositeView
* Removed the `serializeData` method and added directly to `ItemView` and `CompositeView` as needed
-* Modules
- * **BREAKING:** Split module definitions can now receive custom args per module definition, instead of sharing / replacing them across all definitions
-
* Application
* Application regions can now be specified as a jQuery selector string, a region type, or an object literal with a selector and type: `{selector: "#foo", regionType: MyCustomRegion}`
* added `.commands` as instance of Backbone.Wreqr.Commands, to facilitate command execution
* added `.execute` method for direct command execution
* added `.reqres` as instance of Backbone.Wreqr.RequestResponse, to facilitate request/response execution
* added `.request` method for direct requesting of a response
-* triggerMethod
+* Marionette.triggerMethod
* Added `Marionette.triggerMethod` method to trigger an event and call the corresponding method. For example, `view.triggetMethod("before:render")` will trigger the "before:render" event and call the `onBeforeRender` method.
+* Marionette.addEventBinder
+ * Added `Marionette.addEventBinder` method to add all of the Backbone.Wreqr.EventBinder methods to a specified target object
+
* Misc
* jQuery ($) support now works from global `$` or `window.jQuery`
* Updated to Underscore.js v1.4.1
View
41 lib/amd/backbone.marionette.js
@@ -54,6 +54,20 @@
// https://github.com/marionettejs/backbone.eventbinder
Marionette.EventBinder = Backbone.EventBinder;
+ // 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.
+ Marionette.addEventBinder = function(target){
+ var eventBinder = new Marionette.EventBinder();
+ target.eventBinder = eventBinder;
+ target.bindTo = _.bind(eventBinder.bindTo, eventBinder);
+ target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
+ target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
+ };
+
// Marionette.View
// ---------------
@@ -62,7 +76,7 @@
constructor: function(){
_.bindAll(this, "render");
- this._configureEventBinder();
+ Marionette.addEventBinder(this);
Backbone.View.prototype.constructor.apply(this, arguments);
@@ -76,20 +90,6 @@
// 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
@@ -689,8 +689,7 @@
var el = this.options.el;
delete this.options.el;
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ Marionette.addEventBinder(this);
if (el){
this.el = el;
@@ -971,8 +970,9 @@
this.reqres = new Backbone.Wreqr.RequestResponse();
this.submodules = {};
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ _.extend(this, options);
+
+ Marionette.addEventBinder(this);
};
_.extend(Marionette.Application.prototype, Backbone.Events, {
@@ -1124,8 +1124,7 @@
this.config.app = app;
// extend this module with an event binder
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder);
+ Marionette.addEventBinder(this);
};
// Extend the Module prototype with events / bindTo, so that the module
View
2 lib/amd/backbone.marionette.min.js
@@ -2,4 +2,4 @@
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
// http://github.com/marionettejs/backbone.marionette
-(function(e,t){if(typeof exports=="object"){var n=require("jquery"),r=require("underscore"),i=require("backbone");module.exports=t(n,r,i)}else typeof define=="function"&&define.amd&&define(["jquery","underscore","backbone"],t)})(this,function(e,t,n){return n.Marionette=Marionette=function(e,t,n){var r={};r.triggerMethod=function(){var e=Array.prototype.slice.apply(arguments),n=e[0],r=n.split(":"),i,s,o="on";for(var u=0;u<r.length;u++)i=r[u],s=i.charAt(0).toUpperCase(),o+=s+i.slice(1);this.trigger.apply(this,arguments),t.isFunction(this[o])&&(e.shift(),this[o].apply(this,e))},r.EventBinder=e.EventBinder,r.View=e.View.extend({constructor:function(){t.bindAll(this,"render"),this._configureEventBinder(),e.View.prototype.constructor.apply(this,arguments),this.bindBackboneEntityTo(this.model,this.modelEvents),this.bindBackboneEntityTo(this.collection,this.collectionEvents),this.bindTo(this,"show",this.onShowCalled,this)},triggerMethod:r.triggerMethod,_configureEventBinder:function(){var e=new r.EventBinder;this.eventBinder=e,this.bindTo=t.bind(e.bindTo,e),this.unbindFrom=t.bind(e.unbindFrom,e),this.unbindAll=t.bind(e.unbindAll,e)},getTemplate:function(){var e;return this.options&&this.options.template?e=this.options.template:e=this.template,e},mixinTemplateHelpers:function(e){e=e||{};var n=this.templateHelpers;return t.isFunction(n)&&(n=n.call(this)),t.extend(e,n)},configureTriggers:function(){if(!this.triggers)return;var e=this.triggers,n=this,r={};return t.isFunction(e)&&(e=e.call(this)),t.each(e,function(e,t){r[t]=function(t){t&&t.preventDefault&&t.preventDefault(),t&&t.stopPropagation&&t.stopPropagation(),n.trigger(e)}}),r},delegateEvents:function(n){n=n||this.events,t.isFunction(n)&&(n=n.call(this));var r={},i=this.configureTriggers();t.extend(r,n,i),e.View.prototype.delegateEvents.call(this,r)},onShowCalled:function(){},close:function(){if(this.isClosed)return;this.triggerMethod("before:close"),this.remove(),this.unbindAll(),this.triggerMethod("close"),this.isClosed=!0},bindUIElements:function(){if(!this.ui)return;var e=this;this.uiBindings||(this.uiBindings=this.ui),this.ui={},t.each(t.keys(this.uiBindings),function(t){var n=e.uiBindings[t];e.ui[t]=e.$(n)})},bindBackboneEntityTo:function(e,n){if(!e||!n)return;var r=this;t.each(n,function(t,n){var i=r[t];if(!i)throw new Error("View method '"+t+"' was configured as an event handler, but does not exist.");r.bindTo(e,n,i,r)})}}),r.ItemView=r.View.extend({constructor:function(){r.View.prototype.constructor.apply(this,arguments),this.initialEvents&&this.initialEvents()},serializeData:function(){var e;return this.model?e=this.model.toJSON():this.collection&&(e={items:this.collection.toJSON()}),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.triggerMethod("before:render",this),this.triggerMethod("item:before:render",this);var e=this.serializeData(),t=this.getTemplate(),n=r.Renderer.render(t,e);return this.$el.html(n),this.bindUIElements(),this.triggerMethod("render",this),this.triggerMethod("item:rendered",this),this},close:function(){if(this.isClosed)return;this.triggerMethod("item:before:close"),r.View.prototype.close.apply(this,arguments),this.triggerMethod("item:closed")}}),r.CollectionView=r.View.extend({constructor:function(){this.initChildViewStorage(),r.View.prototype.constructor.apply(this,arguments),this.initialEvents(),this.onShowCallbacks=new r.Callbacks},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.render,this))},addChildView:function(e,t,n){this.closeEmptyView();var r=this.getItemView(e),i;return n&&n.index?i=n.index:i=0,this.addItemView(e,r,i)},onShowCalled:function(){this.onShowCallbacks.run()},triggerBeforeRender:function(){this.triggerMethod("before:render",this),this.triggerMethod("collection:before:render",this)},triggerRendered:function(){this.triggerMethod("render",this),this.triggerMethod("collection:rendered",this)},render:function(){return this.isClosed=!1,this.triggerBeforeRender(),this.closeEmptyView(),this.closeChildren(),this.collection&&this.collection.length>0?this.showCollection():this.showEmptyView(),this.triggerRendered(),this},showCollection:function(){var e=this,t;this.collection.each(function(n,r){t=e.getItemView(n),e.addItemView(n,t,r)})},showEmptyView:function(){var t=this.options.emptyView||this.emptyView;if(t&&!this._showingEmptyView){this._showingEmptyView=!0;var n=new e.Model;this.addItemView(n,t,0)}},closeEmptyView:function(){this._showingEmptyView&&(this.closeChildren(),delete this._showingEmptyView)},getItemView:function(e){var t=this.options.itemView||this.itemView;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},addItemView:function(e,t,n){var r=this,s=this.buildItemView(e,t);this.storeChild(s),this.triggerMethod("item:added",s);var o=this.renderItemView(s,n);s.onShow&&this.onShowCallbacks.add(s.onShow,s);var u=this.bindTo(s,"all",function(){var e=i.call(arguments);e[0]="itemview:"+e[0],e.splice(1,0,s),r.triggerMethod.apply(r,e)});return this.childBindings=this.childBindings||{},this.childBindings[s.cid]=u,o},renderItemView:function(e,t){e.render(),this.appendHtml(this,e,t)},buildItemView:function(e,n){var r;t.isFunction(this.itemViewOptions)?r=this.itemViewOptions(e):r=this.itemViewOptions;var i=t.extend({model:e},r),s=new n(i);return s},removeItemView:function(e){var t=this.children[e.cid];if(t){var n=this.childBindings[t.cid];n&&(this.unbindFrom(n),delete this.childBindings[t.cid]),t.close(),delete this.children[e.cid]}(!this.collection||this.collection.length===0)&&this.showEmptyView(),this.triggerMethod("item:removed",t)},appendHtml:function(e,t,n){e.$el.append(t.el)},storeChild:function(e){this.children[e.model.cid]=e},initChildViewStorage:function(){this.children={}},close:function(){if(this.isClosed)return;this.triggerMethod("collection:before:close"),this.closeChildren(),this.triggerMethod("collection:closed"),r.View.prototype.close.apply(this,arguments)},closeChildren:function(){var e=this;this.children&&t.each(t.clone(this.children),function(t){e.removeItemView(t.model)})}}),r.CompositeView=r.CollectionView.extend({constructor:function(e){r.CollectionView.apply(this,arguments),this.itemView=this.getItemView()},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.renderCollection,this))},getItemView:function(e){var t=this.options.itemView||this.itemView||this.constructor;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},serializeData:function(){var e={};return this.model&&(e=this.model.toJSON()),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.resetItemViewContainer();var e=this.renderModel();return this.$el.html(e),this.bindUIElements(),this.triggerMethod("composite:model:rendered"),this.triggerMethod("render"),this.renderCollection(),this.triggerMethod("composite:rendered"),this},renderCollection:function(){r.CollectionView.prototype.render.apply(this,arguments),this.triggerMethod("composite:collection:rendered")},renderModel:function(){var e={};e=this.serializeData();var t=this.getTemplate();return r.Renderer.render(t,e)},appendHtml:function(e,t){var n=this.getItemViewContainer(e);n.append(t.el)},getItemViewContainer:function(e){if("$itemViewContainer"in e)return e.$itemViewContainer;var n;if(e.itemViewContainer){var r=t.result(e,"itemViewContainer");n=e.$(r);if(n.length<=0){var i=new Error("The specified `itemViewContainer` was not found: "+e.itemViewContainer);throw i.name="ItemViewContainerMissingError",i}}else n=e.$el;return e.$itemViewContainer=n,n},resetItemViewContainer:function(){this.$itemViewContainer&&delete this.$itemViewContainer}}),r.Region=function(e){this.options=e||{};var n=this.options.el;delete this.options.el;var i=new r.EventBinder;t.extend(this,i,e),n&&(this.el=n);if(!this.el){var s=new Error("An 'el' must be specified for a region.");throw s.name="NoElError",s}this.initialize&&this.initialize.apply(this,arguments)},t.extend(r.Region,{buildRegion:function(e,t){var n=typeof e=="string",r=typeof e.selector=="string",i=typeof e.regionType=="undefined",s=typeof e=="function";if(!s&&!n&&!r)throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");var o,u;n&&(o=e),e.selector&&(o=e.selector),s&&(u=e),!s&&i&&(u=t),e.regionType&&(u=e.regionType);var a=new u({el:o});return a}}),t.extend(r.Region.prototype,e.Events,{show:function(e){this.ensureEl(),this.close(),e.render(),this.open(e),e.onShow&&e.onShow(),e.trigger("show"),this.onShow&&this.onShow(e),this.trigger("view:show",e),this.currentView=e},ensureEl:function(){if(!this.$el||this.$el.length===0)this.$el=this.getEl(this.el)},getEl:function(e){return n(e)},open:function(e){this.$el.html(e.el)},close:function(){var e=this.currentView;if(!e||e.isClosed)return;e.close&&e.close(),this.trigger("view:closed",e),delete this.currentView},attachView:function(e){this.currentView=e},reset:function(){this.close(),delete this.$el}}),r.Region.extend=e.View.extend,r.Layout=r.ItemView.extend({regionType:r.Region,constructor:function(){this.initializeRegions(),e.Marionette.ItemView.apply(this,arguments)},render:function(){this._firstRender?this._firstRender=!1:(this.closeRegions(),this.reInitializeRegions());var e=r.ItemView.prototype.render.apply(this,arguments);return e},close:function(){if(this.isClosed)return;this.closeRegions(),this.destroyRegions(),e.Marionette.ItemView.prototype.close.call(this,arguments)},initializeRegions:function(){this.regionManagers||(this.regionManagers={});var e=this,n=this.regions||{};t.each(n,function(t,n){var i=r.Region.buildRegion(t,e.regionType);i.getEl=function(t){return e.$(t)},e.regionManagers[n]=i,e[n]=i})},reInitializeRegions:function(){this.regionManagers&&t.size(this.regionManagers)===0?this.initializeRegions():t.each(this.regionManagers,function(e){e.reset()})},closeRegions:function(){var e=this;t.each(this.regionManagers,function(e,t){e.close()})},destroyRegions:function(){var e=this;t.each(this.regionManagers,function(t,n){delete e[n]}),this.regionManagers={}}}),r.Application=function(n){this.initCallbacks=new r.Callbacks,this.vent=new r.EventAggregator,this.commands=new e.Wreqr.Commands,this.reqres=new e.Wreqr.RequestResponse,this.submodules={};var i=new r.EventBinder;t.extend(this,i,n)},t.extend(r.Application.prototype,e.Events,{execute:function(){this.commands.execute.apply(this.commands,arguments)},request:function(){return this.reqres.request.apply(this.reqres,arguments)},addInitializer:function(e){this.initCallbacks.add(e)},start:function(e){this.trigger("initialize:before",e),this.initCallbacks.run(e,this),this.trigger("initialize:after",e),this.trigger("start",e)},addRegions:function(e){var n=this;t.each(e,function(e,t){var i=r.Region.buildRegion(e,r.Region);n[t]=i})},removeRegion:function(e){this[e].close(),delete this[e]},module:function(e,t){var n=i.call(arguments);return n.unshift(this),r.Module.create.apply(r.Module,n)}}),r.Application.extend=e.View.extend,r.AppRouter=e.Router.extend({constructor:function(t){e.Router.prototype.constructor.call(this,t);if(this.appRoutes){var n=this.controller;t&&t.controller&&(n=t.controller),this.processAppRoutes(n,this.appRoutes)}},processAppRoutes:function(e,n){var r,i,s,o,u,a=[],f=this;for(s in n)n.hasOwnProperty(s)&&a.unshift([s,n[s]]);o=a.length;for(u=0;u<o;u++){s=a[u][0],i=a[u][1],r=e[i];if(!r){var l="Method '"+i+"' was not found on the controller",c=new Error(l);throw c.name="NoMethodError",c}r=t.bind(r,e),f.route(s,i,r)}}}),r.Module=function(e,n){this.moduleName=e,this.submodules={},this._setupInitializersAndFinalizers(),this.config={},this.config.app=n;var i=new r.EventBinder;t.extend(this,i)},t.extend(r.Module.prototype,e.Events,{addInitializer:function(e){this._initializerCallbacks.add(e)},addFinalizer:function(e){this._finalizerCallbacks.add(e)},start:function(e){if(this._isInitialized)return;t.each(this.submodules,function(t){t.config.options.startWithParent&&t.start(e)}),this._initializerCallbacks.run(e,this),this._isInitialized=!0},stop:function(){if(!this._isInitialized)return;this._isInitialized=!1,t.each(this.submodules,function(e){e.stop()}),this._finalizerCallbacks.run(),this._initializerCallbacks.reset(),this._finalizerCallbacks.reset()},addDefinition:function(e,t){this._runModuleDefinition(e,t)},_runModuleDefinition:function(i,s){if(!i)return;var o=t.flatten([this,this.config.app,e,r,n,t,s]);i.apply(this,o)},_setupInitializersAndFinalizers:function(){this._initializerCallbacks=new r.Callbacks,this._finalizerCallbacks=new r.Callbacks}}),t.extend(r.Module,{create:function(e,n,r){var s=this,o=e;n=n.split(".");var u=i.apply(arguments);u.splice(0,3);var a=n.length;return t.each(n,function(t,n){var i=n===a-1,f=s._getModuleDefinition(o,t,e);f.config.options=s._getModuleOptions(o,r),i&&s._configureAutoStart(e,f),i&&f.config.options.hasDefinition&&f.addDefinition(f.config.options.definition,u),o=f}),o},_configureAutoStart:function(e,t){t.config.options.startWithParent&&!t.config.autoStartConfigured&&e.addInitializer(function(e){t.start(e)}),t.config.autoStartConfigured=!0},_getModuleDefinition:function(e,t,n){var i=e[t];return i||(i=new r.Module(t,n),e[t]=i,e.submodules[t]=i),i},_getModuleOptions:function(e,n){var r={startWithParent:!0,hasDefinition:!!n};return r.hasDefinition?(t.isFunction(n)?r.definition=n:(r.hasDefinition=!!n.define,r.definition=n.define,n.hasOwnProperty("startWithParent")&&(r.startWithParent=n.startWithParent)),r):r}}),r.TemplateCache=function(e){this.templateId=e},t.extend(r.TemplateCache,{templateCaches:{},get:function(e){var t=this,n=this.templateCaches[e];return n||(n=new r.TemplateCache(e),this.templateCaches[e]=n),n.load()},clear:function(){var e,t=arguments.length;if(t>0)for(e=0;e<t;e++)delete this.templateCaches[arguments[e]];else this.templateCaches={}}}),t.extend(r.TemplateCache.prototype,{load:function(){var e=this;if(this.compiledTemplate)return this.compiledTemplate;var t=this.loadTemplate(this.templateId);return this.compiledTemplate=this.compileTemplate(t),this.compiledTemplate},loadTemplate:function(e){var t=n(e).html();if(!t||t.length===0){var r="Could not find template: '"+e+"'",i=new Error(r);throw i.name="NoTemplateError",i}return t},compileTemplate:function(e){return t.template(e)}}),r.Renderer={render:function(e,t){var n=typeof e=="function"?e:r.TemplateCache.get(e),i=n(t);return i}},r.Callbacks=function(){this._deferred=n.Deferred(),this._callbacks=[]},t.extend(r.Callbacks.prototype,{add:function(e,t){this._callbacks.push({cb:e,ctx:t}),this._deferred.done(function(n,r){t&&(n=t),e.call(n,r)})},run:function(e,t){this._deferred.resolve(t,e)},reset:function(){var e=this,r=this._callbacks;this._deferred=n.Deferred(),this._callbacks=[],t.each(r,function(t){e.add(t.cb,t.ctx)})}}),r.EventAggregator=e.Wreqr.EventAggregator;var i=Array.prototype.slice;return r}(n,t,e||window.jQuery||window.Zepto||window.ender),n.Marionette});
+(function(e,t){if(typeof exports=="object"){var n=require("jquery"),r=require("underscore"),i=require("backbone");module.exports=t(n,r,i)}else typeof define=="function"&&define.amd&&define(["jquery","underscore","backbone"],t)})(this,function(e,t,n){return n.Marionette=Marionette=function(e,t,n){var r={};r.triggerMethod=function(){var e=Array.prototype.slice.apply(arguments),n=e[0],r=n.split(":"),i,s,o="on";for(var u=0;u<r.length;u++)i=r[u],s=i.charAt(0).toUpperCase(),o+=s+i.slice(1);this.trigger.apply(this,arguments),t.isFunction(this[o])&&(e.shift(),this[o].apply(this,e))},r.EventBinder=e.EventBinder,r.addEventBinder=function(e){var n=new r.EventBinder;e.eventBinder=n,e.bindTo=t.bind(n.bindTo,n),e.unbindFrom=t.bind(n.unbindFrom,n),e.unbindAll=t.bind(n.unbindAll,n)},r.View=e.View.extend({constructor:function(){t.bindAll(this,"render"),r.addEventBinder(this),e.View.prototype.constructor.apply(this,arguments),this.bindBackboneEntityTo(this.model,this.modelEvents),this.bindBackboneEntityTo(this.collection,this.collectionEvents),this.bindTo(this,"show",this.onShowCalled,this)},triggerMethod:r.triggerMethod,getTemplate:function(){var e;return this.options&&this.options.template?e=this.options.template:e=this.template,e},mixinTemplateHelpers:function(e){e=e||{};var n=this.templateHelpers;return t.isFunction(n)&&(n=n.call(this)),t.extend(e,n)},configureTriggers:function(){if(!this.triggers)return;var e=this.triggers,n=this,r={};return t.isFunction(e)&&(e=e.call(this)),t.each(e,function(e,t){r[t]=function(t){t&&t.preventDefault&&t.preventDefault(),t&&t.stopPropagation&&t.stopPropagation(),n.trigger(e)}}),r},delegateEvents:function(n){n=n||this.events,t.isFunction(n)&&(n=n.call(this));var r={},i=this.configureTriggers();t.extend(r,n,i),e.View.prototype.delegateEvents.call(this,r)},onShowCalled:function(){},close:function(){if(this.isClosed)return;this.triggerMethod("before:close"),this.remove(),this.unbindAll(),this.triggerMethod("close"),this.isClosed=!0},bindUIElements:function(){if(!this.ui)return;var e=this;this.uiBindings||(this.uiBindings=this.ui),this.ui={},t.each(t.keys(this.uiBindings),function(t){var n=e.uiBindings[t];e.ui[t]=e.$(n)})},bindBackboneEntityTo:function(e,n){if(!e||!n)return;var r=this;t.each(n,function(t,n){var i=r[t];if(!i)throw new Error("View method '"+t+"' was configured as an event handler, but does not exist.");r.bindTo(e,n,i,r)})}}),r.ItemView=r.View.extend({constructor:function(){r.View.prototype.constructor.apply(this,arguments),this.initialEvents&&this.initialEvents()},serializeData:function(){var e;return this.model?e=this.model.toJSON():this.collection&&(e={items:this.collection.toJSON()}),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.triggerMethod("before:render",this),this.triggerMethod("item:before:render",this);var e=this.serializeData(),t=this.getTemplate(),n=r.Renderer.render(t,e);return this.$el.html(n),this.bindUIElements(),this.triggerMethod("render",this),this.triggerMethod("item:rendered",this),this},close:function(){if(this.isClosed)return;this.triggerMethod("item:before:close"),r.View.prototype.close.apply(this,arguments),this.triggerMethod("item:closed")}}),r.CollectionView=r.View.extend({constructor:function(){this.initChildViewStorage(),r.View.prototype.constructor.apply(this,arguments),this.initialEvents(),this.onShowCallbacks=new r.Callbacks},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.render,this))},addChildView:function(e,t,n){this.closeEmptyView();var r=this.getItemView(e),i;return n&&n.index?i=n.index:i=0,this.addItemView(e,r,i)},onShowCalled:function(){this.onShowCallbacks.run()},triggerBeforeRender:function(){this.triggerMethod("before:render",this),this.triggerMethod("collection:before:render",this)},triggerRendered:function(){this.triggerMethod("render",this),this.triggerMethod("collection:rendered",this)},render:function(){return this.isClosed=!1,this.triggerBeforeRender(),this.closeEmptyView(),this.closeChildren(),this.collection&&this.collection.length>0?this.showCollection():this.showEmptyView(),this.triggerRendered(),this},showCollection:function(){var e=this,t;this.collection.each(function(n,r){t=e.getItemView(n),e.addItemView(n,t,r)})},showEmptyView:function(){var t=this.options.emptyView||this.emptyView;if(t&&!this._showingEmptyView){this._showingEmptyView=!0;var n=new e.Model;this.addItemView(n,t,0)}},closeEmptyView:function(){this._showingEmptyView&&(this.closeChildren(),delete this._showingEmptyView)},getItemView:function(e){var t=this.options.itemView||this.itemView;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},addItemView:function(e,t,n){var r=this,s=this.buildItemView(e,t);this.storeChild(s),this.triggerMethod("item:added",s);var o=this.renderItemView(s,n);s.onShow&&this.onShowCallbacks.add(s.onShow,s);var u=this.bindTo(s,"all",function(){var e=i.call(arguments);e[0]="itemview:"+e[0],e.splice(1,0,s),r.triggerMethod.apply(r,e)});return this.childBindings=this.childBindings||{},this.childBindings[s.cid]=u,o},renderItemView:function(e,t){e.render(),this.appendHtml(this,e,t)},buildItemView:function(e,n){var r;t.isFunction(this.itemViewOptions)?r=this.itemViewOptions(e):r=this.itemViewOptions;var i=t.extend({model:e},r),s=new n(i);return s},removeItemView:function(e){var t=this.children[e.cid];if(t){var n=this.childBindings[t.cid];n&&(this.unbindFrom(n),delete this.childBindings[t.cid]),t.close(),delete this.children[e.cid]}(!this.collection||this.collection.length===0)&&this.showEmptyView(),this.triggerMethod("item:removed",t)},appendHtml:function(e,t,n){e.$el.append(t.el)},storeChild:function(e){this.children[e.model.cid]=e},initChildViewStorage:function(){this.children={}},close:function(){if(this.isClosed)return;this.triggerMethod("collection:before:close"),this.closeChildren(),this.triggerMethod("collection:closed"),r.View.prototype.close.apply(this,arguments)},closeChildren:function(){var e=this;this.children&&t.each(t.clone(this.children),function(t){e.removeItemView(t.model)})}}),r.CompositeView=r.CollectionView.extend({constructor:function(e){r.CollectionView.apply(this,arguments),this.itemView=this.getItemView()},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.renderCollection,this))},getItemView:function(e){var t=this.options.itemView||this.itemView||this.constructor;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},serializeData:function(){var e={};return this.model&&(e=this.model.toJSON()),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.resetItemViewContainer();var e=this.renderModel();return this.$el.html(e),this.bindUIElements(),this.triggerMethod("composite:model:rendered"),this.triggerMethod("render"),this.renderCollection(),this.triggerMethod("composite:rendered"),this},renderCollection:function(){r.CollectionView.prototype.render.apply(this,arguments),this.triggerMethod("composite:collection:rendered")},renderModel:function(){var e={};e=this.serializeData();var t=this.getTemplate();return r.Renderer.render(t,e)},appendHtml:function(e,t){var n=this.getItemViewContainer(e);n.append(t.el)},getItemViewContainer:function(e){if("$itemViewContainer"in e)return e.$itemViewContainer;var n;if(e.itemViewContainer){var r=t.result(e,"itemViewContainer");n=e.$(r);if(n.length<=0){var i=new Error("The specified `itemViewContainer` was not found: "+e.itemViewContainer);throw i.name="ItemViewContainerMissingError",i}}else n=e.$el;return e.$itemViewContainer=n,n},resetItemViewContainer:function(){this.$itemViewContainer&&delete this.$itemViewContainer}}),r.Region=function(e){this.options=e||{};var t=this.options.el;delete this.options.el,r.addEventBinder(this),t&&(this.el=t);if(!this.el){var n=new Error("An 'el' must be specified for a region.");throw n.name="NoElError",n}this.initialize&&this.initialize.apply(this,arguments)},t.extend(r.Region,{buildRegion:function(e,t){var n=typeof e=="string",r=typeof e.selector=="string",i=typeof e.regionType=="undefined",s=typeof e=="function";if(!s&&!n&&!r)throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");var o,u;n&&(o=e),e.selector&&(o=e.selector),s&&(u=e),!s&&i&&(u=t),e.regionType&&(u=e.regionType);var a=new u({el:o});return a}}),t.extend(r.Region.prototype,e.Events,{show:function(e){this.ensureEl(),this.close(),e.render(),this.open(e),e.onShow&&e.onShow(),e.trigger("show"),this.onShow&&this.onShow(e),this.trigger("view:show",e),this.currentView=e},ensureEl:function(){if(!this.$el||this.$el.length===0)this.$el=this.getEl(this.el)},getEl:function(e){return n(e)},open:function(e){this.$el.html(e.el)},close:function(){var e=this.currentView;if(!e||e.isClosed)return;e.close&&e.close(),this.trigger("view:closed",e),delete this.currentView},attachView:function(e){this.currentView=e},reset:function(){this.close(),delete this.$el}}),r.Region.extend=e.View.extend,r.Layout=r.ItemView.extend({regionType:r.Region,constructor:function(){this.initializeRegions(),e.Marionette.ItemView.apply(this,arguments)},render:function(){this._firstRender?this._firstRender=!1:(this.closeRegions(),this.reInitializeRegions());var e=r.ItemView.prototype.render.apply(this,arguments);return e},close:function(){if(this.isClosed)return;this.closeRegions(),this.destroyRegions(),e.Marionette.ItemView.prototype.close.call(this,arguments)},initializeRegions:function(){this.regionManagers||(this.regionManagers={});var e=this,n=this.regions||{};t.each(n,function(t,n){var i=r.Region.buildRegion(t,e.regionType);i.getEl=function(t){return e.$(t)},e.regionManagers[n]=i,e[n]=i})},reInitializeRegions:function(){this.regionManagers&&t.size(this.regionManagers)===0?this.initializeRegions():t.each(this.regionManagers,function(e){e.reset()})},closeRegions:function(){var e=this;t.each(this.regionManagers,function(e,t){e.close()})},destroyRegions:function(){var e=this;t.each(this.regionManagers,function(t,n){delete e[n]}),this.regionManagers={}}}),r.Application=function(n){this.initCallbacks=new r.Callbacks,this.vent=new r.EventAggregator,this.commands=new e.Wreqr.Commands,this.reqres=new e.Wreqr.RequestResponse,this.submodules={},t.extend(this,n),r.addEventBinder(this)},t.extend(r.Application.prototype,e.Events,{execute:function(){this.commands.execute.apply(this.commands,arguments)},request:function(){return this.reqres.request.apply(this.reqres,arguments)},addInitializer:function(e){this.initCallbacks.add(e)},start:function(e){this.trigger("initialize:before",e),this.initCallbacks.run(e,this),this.trigger("initialize:after",e),this.trigger("start",e)},addRegions:function(e){var n=this;t.each(e,function(e,t){var i=r.Region.buildRegion(e,r.Region);n[t]=i})},removeRegion:function(e){this[e].close(),delete this[e]},module:function(e,t){var n=i.call(arguments);return n.unshift(this),r.Module.create.apply(r.Module,n)}}),r.Application.extend=e.View.extend,r.AppRouter=e.Router.extend({constructor:function(t){e.Router.prototype.constructor.call(this,t);if(this.appRoutes){var n=this.controller;t&&t.controller&&(n=t.controller),this.processAppRoutes(n,this.appRoutes)}},processAppRoutes:function(e,n){var r,i,s,o,u,a=[],f=this;for(s in n)n.hasOwnProperty(s)&&a.unshift([s,n[s]]);o=a.length;for(u=0;u<o;u++){s=a[u][0],i=a[u][1],r=e[i];if(!r){var l="Method '"+i+"' was not found on the controller",c=new Error(l);throw c.name="NoMethodError",c}r=t.bind(r,e),f.route(s,i,r)}}}),r.Module=function(e,t){this.moduleName=e,this.submodules={},this._setupInitializersAndFinalizers(),this.config={},this.config.app=t,r.addEventBinder(this)},t.extend(r.Module.prototype,e.Events,{addInitializer:function(e){this._initializerCallbacks.add(e)},addFinalizer:function(e){this._finalizerCallbacks.add(e)},start:function(e){if(this._isInitialized)return;t.each(this.submodules,function(t){t.config.options.startWithParent&&t.start(e)}),this._initializerCallbacks.run(e,this),this._isInitialized=!0},stop:function(){if(!this._isInitialized)return;this._isInitialized=!1,t.each(this.submodules,function(e){e.stop()}),this._finalizerCallbacks.run(),this._initializerCallbacks.reset(),this._finalizerCallbacks.reset()},addDefinition:function(e,t){this._runModuleDefinition(e,t)},_runModuleDefinition:function(i,s){if(!i)return;var o=t.flatten([this,this.config.app,e,r,n,t,s]);i.apply(this,o)},_setupInitializersAndFinalizers:function(){this._initializerCallbacks=new r.Callbacks,this._finalizerCallbacks=new r.Callbacks}}),t.extend(r.Module,{create:function(e,n,r){var s=this,o=e;n=n.split(".");var u=i.apply(arguments);u.splice(0,3);var a=n.length;return t.each(n,function(t,n){var i=n===a-1,f=s._getModuleDefinition(o,t,e);f.config.options=s._getModuleOptions(o,r),i&&s._configureAutoStart(e,f),i&&f.config.options.hasDefinition&&f.addDefinition(f.config.options.definition,u),o=f}),o},_configureAutoStart:function(e,t){t.config.options.startWithParent&&!t.config.autoStartConfigured&&e.addInitializer(function(e){t.start(e)}),t.config.autoStartConfigured=!0},_getModuleDefinition:function(e,t,n){var i=e[t];return i||(i=new r.Module(t,n),e[t]=i,e.submodules[t]=i),i},_getModuleOptions:function(e,n){var r={startWithParent:!0,hasDefinition:!!n};return r.hasDefinition?(t.isFunction(n)?r.definition=n:(r.hasDefinition=!!n.define,r.definition=n.define,n.hasOwnProperty("startWithParent")&&(r.startWithParent=n.startWithParent)),r):r}}),r.TemplateCache=function(e){this.templateId=e},t.extend(r.TemplateCache,{templateCaches:{},get:function(e){var t=this,n=this.templateCaches[e];return n||(n=new r.TemplateCache(e),this.templateCaches[e]=n),n.load()},clear:function(){var e,t=arguments.length;if(t>0)for(e=0;e<t;e++)delete this.templateCaches[arguments[e]];else this.templateCaches={}}}),t.extend(r.TemplateCache.prototype,{load:function(){var e=this;if(this.compiledTemplate)return this.compiledTemplate;var t=this.loadTemplate(this.templateId);return this.compiledTemplate=this.compileTemplate(t),this.compiledTemplate},loadTemplate:function(e){var t=n(e).html();if(!t||t.length===0){var r="Could not find template: '"+e+"'",i=new Error(r);throw i.name="NoTemplateError",i}return t},compileTemplate:function(e){return t.template(e)}}),r.Renderer={render:function(e,t){var n=typeof e=="function"?e:r.TemplateCache.get(e),i=n(t);return i}},r.Callbacks=function(){this._deferred=n.Deferred(),this._callbacks=[]},t.extend(r.Callbacks.prototype,{add:function(e,t){this._callbacks.push({cb:e,ctx:t}),this._deferred.done(function(n,r){t&&(n=t),e.call(n,r)})},run:function(e,t){this._deferred.resolve(t,e)},reset:function(){var e=this,r=this._callbacks;this._deferred=n.Deferred(),this._callbacks=[],t.each(r,function(t){e.add(t.cb,t.ctx)})}}),r.EventAggregator=e.Wreqr.EventAggregator;var i=Array.prototype.slice;return r}(n,t,e||window.jQuery||window.Zepto||window.ender),n.Marionette});
View
41 lib/backbone.marionette.js
@@ -38,6 +38,20 @@ Marionette.triggerMethod = function(){
// https://github.com/marionettejs/backbone.eventbinder
Marionette.EventBinder = Backbone.EventBinder;
+// 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.
+Marionette.addEventBinder = function(target){
+ var eventBinder = new Marionette.EventBinder();
+ target.eventBinder = eventBinder;
+ target.bindTo = _.bind(eventBinder.bindTo, eventBinder);
+ target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
+ target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
+};
+
// Marionette.View
// ---------------
@@ -46,7 +60,7 @@ Marionette.View = Backbone.View.extend({
constructor: function(){
_.bindAll(this, "render");
- this._configureEventBinder();
+ Marionette.addEventBinder(this);
Backbone.View.prototype.constructor.apply(this, arguments);
@@ -60,20 +74,6 @@ 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
@@ -673,8 +673,7 @@ Marionette.Region = function(options){
var el = this.options.el;
delete this.options.el;
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ Marionette.addEventBinder(this);
if (el){
this.el = el;
@@ -955,8 +954,9 @@ Marionette.Application = function(options){
this.reqres = new Backbone.Wreqr.RequestResponse();
this.submodules = {};
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ _.extend(this, options);
+
+ Marionette.addEventBinder(this);
};
_.extend(Marionette.Application.prototype, Backbone.Events, {
@@ -1108,8 +1108,7 @@ Marionette.Module = function(moduleName, app){
this.config.app = app;
// extend this module with an event binder
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder);
+ Marionette.addEventBinder(this);
};
// Extend the Module prototype with events / bindTo, so that the module
View
2 lib/backbone.marionette.min.js
@@ -2,4 +2,4 @@
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
// http://github.com/marionettejs/backbone.marionette
-Backbone.Marionette=Marionette=function(e,t,n){var r={};r.triggerMethod=function(){var e=Array.prototype.slice.apply(arguments),n=e[0],r=n.split(":"),i,s,o="on";for(var u=0;u<r.length;u++)i=r[u],s=i.charAt(0).toUpperCase(),o+=s+i.slice(1);this.trigger.apply(this,arguments),t.isFunction(this[o])&&(e.shift(),this[o].apply(this,e))},r.EventBinder=e.EventBinder,r.View=e.View.extend({constructor:function(){t.bindAll(this,"render"),this._configureEventBinder(),e.View.prototype.constructor.apply(this,arguments),this.bindBackboneEntityTo(this.model,this.modelEvents),this.bindBackboneEntityTo(this.collection,this.collectionEvents),this.bindTo(this,"show",this.onShowCalled,this)},triggerMethod:r.triggerMethod,_configureEventBinder:function(){var e=new r.EventBinder;this.eventBinder=e,this.bindTo=t.bind(e.bindTo,e),this.unbindFrom=t.bind(e.unbindFrom,e),this.unbindAll=t.bind(e.unbindAll,e)},getTemplate:function(){var e;return this.options&&this.options.template?e=this.options.template:e=this.template,e},mixinTemplateHelpers:function(e){e=e||{};var n=this.templateHelpers;return t.isFunction(n)&&(n=n.call(this)),t.extend(e,n)},configureTriggers:function(){if(!this.triggers)return;var e=this.triggers,n=this,r={};return t.isFunction(e)&&(e=e.call(this)),t.each(e,function(e,t){r[t]=function(t){t&&t.preventDefault&&t.preventDefault(),t&&t.stopPropagation&&t.stopPropagation(),n.trigger(e)}}),r},delegateEvents:function(n){n=n||this.events,t.isFunction(n)&&(n=n.call(this));var r={},i=this.configureTriggers();t.extend(r,n,i),e.View.prototype.delegateEvents.call(this,r)},onShowCalled:function(){},close:function(){if(this.isClosed)return;this.triggerMethod("before:close"),this.remove(),this.unbindAll(),this.triggerMethod("close"),this.isClosed=!0},bindUIElements:function(){if(!this.ui)return;var e=this;this.uiBindings||(this.uiBindings=this.ui),this.ui={},t.each(t.keys(this.uiBindings),function(t){var n=e.uiBindings[t];e.ui[t]=e.$(n)})},bindBackboneEntityTo:function(e,n){if(!e||!n)return;var r=this;t.each(n,function(t,n){var i=r[t];if(!i)throw new Error("View method '"+t+"' was configured as an event handler, but does not exist.");r.bindTo(e,n,i,r)})}}),r.ItemView=r.View.extend({constructor:function(){r.View.prototype.constructor.apply(this,arguments),this.initialEvents&&this.initialEvents()},serializeData:function(){var e;return this.model?e=this.model.toJSON():this.collection&&(e={items:this.collection.toJSON()}),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.triggerMethod("before:render",this),this.triggerMethod("item:before:render",this);var e=this.serializeData(),t=this.getTemplate(),n=r.Renderer.render(t,e);return this.$el.html(n),this.bindUIElements(),this.triggerMethod("render",this),this.triggerMethod("item:rendered",this),this},close:function(){if(this.isClosed)return;this.triggerMethod("item:before:close"),r.View.prototype.close.apply(this,arguments),this.triggerMethod("item:closed")}}),r.CollectionView=r.View.extend({constructor:function(){this.initChildViewStorage(),r.View.prototype.constructor.apply(this,arguments),this.initialEvents(),this.onShowCallbacks=new r.Callbacks},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.render,this))},addChildView:function(e,t,n){this.closeEmptyView();var r=this.getItemView(e),i;return n&&n.index?i=n.index:i=0,this.addItemView(e,r,i)},onShowCalled:function(){this.onShowCallbacks.run()},triggerBeforeRender:function(){this.triggerMethod("before:render",this),this.triggerMethod("collection:before:render",this)},triggerRendered:function(){this.triggerMethod("render",this),this.triggerMethod("collection:rendered",this)},render:function(){return this.isClosed=!1,this.triggerBeforeRender(),this.closeEmptyView(),this.closeChildren(),this.collection&&this.collection.length>0?this.showCollection():this.showEmptyView(),this.triggerRendered(),this},showCollection:function(){var e=this,t;this.collection.each(function(n,r){t=e.getItemView(n),e.addItemView(n,t,r)})},showEmptyView:function(){var t=this.options.emptyView||this.emptyView;if(t&&!this._showingEmptyView){this._showingEmptyView=!0;var n=new e.Model;this.addItemView(n,t,0)}},closeEmptyView:function(){this._showingEmptyView&&(this.closeChildren(),delete this._showingEmptyView)},getItemView:function(e){var t=this.options.itemView||this.itemView;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},addItemView:function(e,t,n){var r=this,s=this.buildItemView(e,t);this.storeChild(s),this.triggerMethod("item:added",s);var o=this.renderItemView(s,n);s.onShow&&this.onShowCallbacks.add(s.onShow,s);var u=this.bindTo(s,"all",function(){var e=i.call(arguments);e[0]="itemview:"+e[0],e.splice(1,0,s),r.triggerMethod.apply(r,e)});return this.childBindings=this.childBindings||{},this.childBindings[s.cid]=u,o},renderItemView:function(e,t){e.render(),this.appendHtml(this,e,t)},buildItemView:function(e,n){var r;t.isFunction(this.itemViewOptions)?r=this.itemViewOptions(e):r=this.itemViewOptions;var i=t.extend({model:e},r),s=new n(i);return s},removeItemView:function(e){var t=this.children[e.cid];if(t){var n=this.childBindings[t.cid];n&&(this.unbindFrom(n),delete this.childBindings[t.cid]),t.close(),delete this.children[e.cid]}(!this.collection||this.collection.length===0)&&this.showEmptyView(),this.triggerMethod("item:removed",t)},appendHtml:function(e,t,n){e.$el.append(t.el)},storeChild:function(e){this.children[e.model.cid]=e},initChildViewStorage:function(){this.children={}},close:function(){if(this.isClosed)return;this.triggerMethod("collection:before:close"),this.closeChildren(),this.triggerMethod("collection:closed"),r.View.prototype.close.apply(this,arguments)},closeChildren:function(){var e=this;this.children&&t.each(t.clone(this.children),function(t){e.removeItemView(t.model)})}}),r.CompositeView=r.CollectionView.extend({constructor:function(e){r.CollectionView.apply(this,arguments),this.itemView=this.getItemView()},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.renderCollection,this))},getItemView:function(e){var t=this.options.itemView||this.itemView||this.constructor;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},serializeData:function(){var e={};return this.model&&(e=this.model.toJSON()),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.resetItemViewContainer();var e=this.renderModel();return this.$el.html(e),this.bindUIElements(),this.triggerMethod("composite:model:rendered"),this.triggerMethod("render"),this.renderCollection(),this.triggerMethod("composite:rendered"),this},renderCollection:function(){r.CollectionView.prototype.render.apply(this,arguments),this.triggerMethod("composite:collection:rendered")},renderModel:function(){var e={};e=this.serializeData();var t=this.getTemplate();return r.Renderer.render(t,e)},appendHtml:function(e,t){var n=this.getItemViewContainer(e);n.append(t.el)},getItemViewContainer:function(e){if("$itemViewContainer"in e)return e.$itemViewContainer;var n;if(e.itemViewContainer){var r=t.result(e,"itemViewContainer");n=e.$(r);if(n.length<=0){var i=new Error("The specified `itemViewContainer` was not found: "+e.itemViewContainer);throw i.name="ItemViewContainerMissingError",i}}else n=e.$el;return e.$itemViewContainer=n,n},resetItemViewContainer:function(){this.$itemViewContainer&&delete this.$itemViewContainer}}),r.Region=function(e){this.options=e||{};var n=this.options.el;delete this.options.el;var i=new r.EventBinder;t.extend(this,i,e),n&&(this.el=n);if(!this.el){var s=new Error("An 'el' must be specified for a region.");throw s.name="NoElError",s}this.initialize&&this.initialize.apply(this,arguments)},t.extend(r.Region,{buildRegion:function(e,t){var n=typeof e=="string",r=typeof e.selector=="string",i=typeof e.regionType=="undefined",s=typeof e=="function";if(!s&&!n&&!r)throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");var o,u;n&&(o=e),e.selector&&(o=e.selector),s&&(u=e),!s&&i&&(u=t),e.regionType&&(u=e.regionType);var a=new u({el:o});return a}}),t.extend(r.Region.prototype,e.Events,{show:function(e){this.ensureEl(),this.close(),e.render(),this.open(e),e.onShow&&e.onShow(),e.trigger("show"),this.onShow&&this.onShow(e),this.trigger("view:show",e),this.currentView=e},ensureEl:function(){if(!this.$el||this.$el.length===0)this.$el=this.getEl(this.el)},getEl:function(e){return n(e)},open:function(e){this.$el.html(e.el)},close:function(){var e=this.currentView;if(!e||e.isClosed)return;e.close&&e.close(),this.trigger("view:closed",e),delete this.currentView},attachView:function(e){this.currentView=e},reset:function(){this.close(),delete this.$el}}),r.Region.extend=e.View.extend,r.Layout=r.ItemView.extend({regionType:r.Region,constructor:function(){this.initializeRegions(),e.Marionette.ItemView.apply(this,arguments)},render:function(){this._firstRender?this._firstRender=!1:(this.closeRegions(),this.reInitializeRegions());var e=r.ItemView.prototype.render.apply(this,arguments);return e},close:function(){if(this.isClosed)return;this.closeRegions(),this.destroyRegions(),e.Marionette.ItemView.prototype.close.call(this,arguments)},initializeRegions:function(){this.regionManagers||(this.regionManagers={});var e=this,n=this.regions||{};t.each(n,function(t,n){var i=r.Region.buildRegion(t,e.regionType);i.getEl=function(t){return e.$(t)},e.regionManagers[n]=i,e[n]=i})},reInitializeRegions:function(){this.regionManagers&&t.size(this.regionManagers)===0?this.initializeRegions():t.each(this.regionManagers,function(e){e.reset()})},closeRegions:function(){var e=this;t.each(this.regionManagers,function(e,t){e.close()})},destroyRegions:function(){var e=this;t.each(this.regionManagers,function(t,n){delete e[n]}),this.regionManagers={}}}),r.Application=function(n){this.initCallbacks=new r.Callbacks,this.vent=new r.EventAggregator,this.commands=new e.Wreqr.Commands,this.reqres=new e.Wreqr.RequestResponse,this.submodules={};var i=new r.EventBinder;t.extend(this,i,n)},t.extend(r.Application.prototype,e.Events,{execute:function(){this.commands.execute.apply(this.commands,arguments)},request:function(){return this.reqres.request.apply(this.reqres,arguments)},addInitializer:function(e){this.initCallbacks.add(e)},start:function(e){this.trigger("initialize:before",e),this.initCallbacks.run(e,this),this.trigger("initialize:after",e),this.trigger("start",e)},addRegions:function(e){var n=this;t.each(e,function(e,t){var i=r.Region.buildRegion(e,r.Region);n[t]=i})},removeRegion:function(e){this[e].close(),delete this[e]},module:function(e,t){var n=i.call(arguments);return n.unshift(this),r.Module.create.apply(r.Module,n)}}),r.Application.extend=e.View.extend,r.AppRouter=e.Router.extend({constructor:function(t){e.Router.prototype.constructor.call(this,t);if(this.appRoutes){var n=this.controller;t&&t.controller&&(n=t.controller),this.processAppRoutes(n,this.appRoutes)}},processAppRoutes:function(e,n){var r,i,s,o,u,a=[],f=this;for(s in n)n.hasOwnProperty(s)&&a.unshift([s,n[s]]);o=a.length;for(u=0;u<o;u++){s=a[u][0],i=a[u][1],r=e[i];if(!r){var l="Method '"+i+"' was not found on the controller",c=new Error(l);throw c.name="NoMethodError",c}r=t.bind(r,e),f.route(s,i,r)}}}),r.Module=function(e,n){this.moduleName=e,this.submodules={},this._setupInitializersAndFinalizers(),this.config={},this.config.app=n;var i=new r.EventBinder;t.extend(this,i)},t.extend(r.Module.prototype,e.Events,{addInitializer:function(e){this._initializerCallbacks.add(e)},addFinalizer:function(e){this._finalizerCallbacks.add(e)},start:function(e){if(this._isInitialized)return;t.each(this.submodules,function(t){t.config.options.startWithParent&&t.start(e)}),this._initializerCallbacks.run(e,this),this._isInitialized=!0},stop:function(){if(!this._isInitialized)return;this._isInitialized=!1,t.each(this.submodules,function(e){e.stop()}),this._finalizerCallbacks.run(),this._initializerCallbacks.reset(),this._finalizerCallbacks.reset()},addDefinition:function(e,t){this._runModuleDefinition(e,t)},_runModuleDefinition:function(i,s){if(!i)return;var o=t.flatten([this,this.config.app,e,r,n,t,s]);i.apply(this,o)},_setupInitializersAndFinalizers:function(){this._initializerCallbacks=new r.Callbacks,this._finalizerCallbacks=new r.Callbacks}}),t.extend(r.Module,{create:function(e,n,r){var s=this,o=e;n=n.split(".");var u=i.apply(arguments);u.splice(0,3);var a=n.length;return t.each(n,function(t,n){var i=n===a-1,f=s._getModuleDefinition(o,t,e);f.config.options=s._getModuleOptions(o,r),i&&s._configureAutoStart(e,f),i&&f.config.options.hasDefinition&&f.addDefinition(f.config.options.definition,u),o=f}),o},_configureAutoStart:function(e,t){t.config.options.startWithParent&&!t.config.autoStartConfigured&&e.addInitializer(function(e){t.start(e)}),t.config.autoStartConfigured=!0},_getModuleDefinition:function(e,t,n){var i=e[t];return i||(i=new r.Module(t,n),e[t]=i,e.submodules[t]=i),i},_getModuleOptions:function(e,n){var r={startWithParent:!0,hasDefinition:!!n};return r.hasDefinition?(t.isFunction(n)?r.definition=n:(r.hasDefinition=!!n.define,r.definition=n.define,n.hasOwnProperty("startWithParent")&&(r.startWithParent=n.startWithParent)),r):r}}),r.TemplateCache=function(e){this.templateId=e},t.extend(r.TemplateCache,{templateCaches:{},get:function(e){var t=this,n=this.templateCaches[e];return n||(n=new r.TemplateCache(e),this.templateCaches[e]=n),n.load()},clear:function(){var e,t=arguments.length;if(t>0)for(e=0;e<t;e++)delete this.templateCaches[arguments[e]];else this.templateCaches={}}}),t.extend(r.TemplateCache.prototype,{load:function(){var e=this;if(this.compiledTemplate)return this.compiledTemplate;var t=this.loadTemplate(this.templateId);return this.compiledTemplate=this.compileTemplate(t),this.compiledTemplate},loadTemplate:function(e){var t=n(e).html();if(!t||t.length===0){var r="Could not find template: '"+e+"'",i=new Error(r);throw i.name="NoTemplateError",i}return t},compileTemplate:function(e){return t.template(e)}}),r.Renderer={render:function(e,t){var n=typeof e=="function"?e:r.TemplateCache.get(e),i=n(t);return i}},r.Callbacks=function(){this._deferred=n.Deferred(),this._callbacks=[]},t.extend(r.Callbacks.prototype,{add:function(e,t){this._callbacks.push({cb:e,ctx:t}),this._deferred.done(function(n,r){t&&(n=t),e.call(n,r)})},run:function(e,t){this._deferred.resolve(t,e)},reset:function(){var e=this,r=this._callbacks;this._deferred=n.Deferred(),this._callbacks=[],t.each(r,function(t){e.add(t.cb,t.ctx)})}}),r.EventAggregator=e.Wreqr.EventAggregator;var i=Array.prototype.slice;return r}(Backbone,_,$||window.jQuery||window.Zepto||window.ender);
+Backbone.Marionette=Marionette=function(e,t,n){var r={};r.triggerMethod=function(){var e=Array.prototype.slice.apply(arguments),n=e[0],r=n.split(":"),i,s,o="on";for(var u=0;u<r.length;u++)i=r[u],s=i.charAt(0).toUpperCase(),o+=s+i.slice(1);this.trigger.apply(this,arguments),t.isFunction(this[o])&&(e.shift(),this[o].apply(this,e))},r.EventBinder=e.EventBinder,r.addEventBinder=function(e){var n=new r.EventBinder;e.eventBinder=n,e.bindTo=t.bind(n.bindTo,n),e.unbindFrom=t.bind(n.unbindFrom,n),e.unbindAll=t.bind(n.unbindAll,n)},r.View=e.View.extend({constructor:function(){t.bindAll(this,"render"),r.addEventBinder(this),e.View.prototype.constructor.apply(this,arguments),this.bindBackboneEntityTo(this.model,this.modelEvents),this.bindBackboneEntityTo(this.collection,this.collectionEvents),this.bindTo(this,"show",this.onShowCalled,this)},triggerMethod:r.triggerMethod,getTemplate:function(){var e;return this.options&&this.options.template?e=this.options.template:e=this.template,e},mixinTemplateHelpers:function(e){e=e||{};var n=this.templateHelpers;return t.isFunction(n)&&(n=n.call(this)),t.extend(e,n)},configureTriggers:function(){if(!this.triggers)return;var e=this.triggers,n=this,r={};return t.isFunction(e)&&(e=e.call(this)),t.each(e,function(e,t){r[t]=function(t){t&&t.preventDefault&&t.preventDefault(),t&&t.stopPropagation&&t.stopPropagation(),n.trigger(e)}}),r},delegateEvents:function(n){n=n||this.events,t.isFunction(n)&&(n=n.call(this));var r={},i=this.configureTriggers();t.extend(r,n,i),e.View.prototype.delegateEvents.call(this,r)},onShowCalled:function(){},close:function(){if(this.isClosed)return;this.triggerMethod("before:close"),this.remove(),this.unbindAll(),this.triggerMethod("close"),this.isClosed=!0},bindUIElements:function(){if(!this.ui)return;var e=this;this.uiBindings||(this.uiBindings=this.ui),this.ui={},t.each(t.keys(this.uiBindings),function(t){var n=e.uiBindings[t];e.ui[t]=e.$(n)})},bindBackboneEntityTo:function(e,n){if(!e||!n)return;var r=this;t.each(n,function(t,n){var i=r[t];if(!i)throw new Error("View method '"+t+"' was configured as an event handler, but does not exist.");r.bindTo(e,n,i,r)})}}),r.ItemView=r.View.extend({constructor:function(){r.View.prototype.constructor.apply(this,arguments),this.initialEvents&&this.initialEvents()},serializeData:function(){var e;return this.model?e=this.model.toJSON():this.collection&&(e={items:this.collection.toJSON()}),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.triggerMethod("before:render",this),this.triggerMethod("item:before:render",this);var e=this.serializeData(),t=this.getTemplate(),n=r.Renderer.render(t,e);return this.$el.html(n),this.bindUIElements(),this.triggerMethod("render",this),this.triggerMethod("item:rendered",this),this},close:function(){if(this.isClosed)return;this.triggerMethod("item:before:close"),r.View.prototype.close.apply(this,arguments),this.triggerMethod("item:closed")}}),r.CollectionView=r.View.extend({constructor:function(){this.initChildViewStorage(),r.View.prototype.constructor.apply(this,arguments),this.initialEvents(),this.onShowCallbacks=new r.Callbacks},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.render,this))},addChildView:function(e,t,n){this.closeEmptyView();var r=this.getItemView(e),i;return n&&n.index?i=n.index:i=0,this.addItemView(e,r,i)},onShowCalled:function(){this.onShowCallbacks.run()},triggerBeforeRender:function(){this.triggerMethod("before:render",this),this.triggerMethod("collection:before:render",this)},triggerRendered:function(){this.triggerMethod("render",this),this.triggerMethod("collection:rendered",this)},render:function(){return this.isClosed=!1,this.triggerBeforeRender(),this.closeEmptyView(),this.closeChildren(),this.collection&&this.collection.length>0?this.showCollection():this.showEmptyView(),this.triggerRendered(),this},showCollection:function(){var e=this,t;this.collection.each(function(n,r){t=e.getItemView(n),e.addItemView(n,t,r)})},showEmptyView:function(){var t=this.options.emptyView||this.emptyView;if(t&&!this._showingEmptyView){this._showingEmptyView=!0;var n=new e.Model;this.addItemView(n,t,0)}},closeEmptyView:function(){this._showingEmptyView&&(this.closeChildren(),delete this._showingEmptyView)},getItemView:function(e){var t=this.options.itemView||this.itemView;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},addItemView:function(e,t,n){var r=this,s=this.buildItemView(e,t);this.storeChild(s),this.triggerMethod("item:added",s);var o=this.renderItemView(s,n);s.onShow&&this.onShowCallbacks.add(s.onShow,s);var u=this.bindTo(s,"all",function(){var e=i.call(arguments);e[0]="itemview:"+e[0],e.splice(1,0,s),r.triggerMethod.apply(r,e)});return this.childBindings=this.childBindings||{},this.childBindings[s.cid]=u,o},renderItemView:function(e,t){e.render(),this.appendHtml(this,e,t)},buildItemView:function(e,n){var r;t.isFunction(this.itemViewOptions)?r=this.itemViewOptions(e):r=this.itemViewOptions;var i=t.extend({model:e},r),s=new n(i);return s},removeItemView:function(e){var t=this.children[e.cid];if(t){var n=this.childBindings[t.cid];n&&(this.unbindFrom(n),delete this.childBindings[t.cid]),t.close(),delete this.children[e.cid]}(!this.collection||this.collection.length===0)&&this.showEmptyView(),this.triggerMethod("item:removed",t)},appendHtml:function(e,t,n){e.$el.append(t.el)},storeChild:function(e){this.children[e.model.cid]=e},initChildViewStorage:function(){this.children={}},close:function(){if(this.isClosed)return;this.triggerMethod("collection:before:close"),this.closeChildren(),this.triggerMethod("collection:closed"),r.View.prototype.close.apply(this,arguments)},closeChildren:function(){var e=this;this.children&&t.each(t.clone(this.children),function(t){e.removeItemView(t.model)})}}),r.CompositeView=r.CollectionView.extend({constructor:function(e){r.CollectionView.apply(this,arguments),this.itemView=this.getItemView()},initialEvents:function(){this.collection&&(this.bindTo(this.collection,"add",this.addChildView,this),this.bindTo(this.collection,"remove",this.removeItemView,this),this.bindTo(this.collection,"reset",this.renderCollection,this))},getItemView:function(e){var t=this.options.itemView||this.itemView||this.constructor;if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},serializeData:function(){var e={};return this.model&&(e=this.model.toJSON()),e=this.mixinTemplateHelpers(e),e},render:function(){this.isClosed=!1,this.resetItemViewContainer();var e=this.renderModel();return this.$el.html(e),this.bindUIElements(),this.triggerMethod("composite:model:rendered"),this.triggerMethod("render"),this.renderCollection(),this.triggerMethod("composite:rendered"),this},renderCollection:function(){r.CollectionView.prototype.render.apply(this,arguments),this.triggerMethod("composite:collection:rendered")},renderModel:function(){var e={};e=this.serializeData();var t=this.getTemplate();return r.Renderer.render(t,e)},appendHtml:function(e,t){var n=this.getItemViewContainer(e);n.append(t.el)},getItemViewContainer:function(e){if("$itemViewContainer"in e)return e.$itemViewContainer;var n;if(e.itemViewContainer){var r=t.result(e,"itemViewContainer");n=e.$(r);if(n.length<=0){var i=new Error("The specified `itemViewContainer` was not found: "+e.itemViewContainer);throw i.name="ItemViewContainerMissingError",i}}else n=e.$el;return e.$itemViewContainer=n,n},resetItemViewContainer:function(){this.$itemViewContainer&&delete this.$itemViewContainer}}),r.Region=function(e){this.options=e||{};var t=this.options.el;delete this.options.el,r.addEventBinder(this),t&&(this.el=t);if(!this.el){var n=new Error("An 'el' must be specified for a region.");throw n.name="NoElError",n}this.initialize&&this.initialize.apply(this,arguments)},t.extend(r.Region,{buildRegion:function(e,t){var n=typeof e=="string",r=typeof e.selector=="string",i=typeof e.regionType=="undefined",s=typeof e=="function";if(!s&&!n&&!r)throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");var o,u;n&&(o=e),e.selector&&(o=e.selector),s&&(u=e),!s&&i&&(u=t),e.regionType&&(u=e.regionType);var a=new u({el:o});return a}}),t.extend(r.Region.prototype,e.Events,{show:function(e){this.ensureEl(),this.close(),e.render(),this.open(e),e.onShow&&e.onShow(),e.trigger("show"),this.onShow&&this.onShow(e),this.trigger("view:show",e),this.currentView=e},ensureEl:function(){if(!this.$el||this.$el.length===0)this.$el=this.getEl(this.el)},getEl:function(e){return n(e)},open:function(e){this.$el.html(e.el)},close:function(){var e=this.currentView;if(!e||e.isClosed)return;e.close&&e.close(),this.trigger("view:closed",e),delete this.currentView},attachView:function(e){this.currentView=e},reset:function(){this.close(),delete this.$el}}),r.Region.extend=e.View.extend,r.Layout=r.ItemView.extend({regionType:r.Region,constructor:function(){this.initializeRegions(),e.Marionette.ItemView.apply(this,arguments)},render:function(){this._firstRender?this._firstRender=!1:(this.closeRegions(),this.reInitializeRegions());var e=r.ItemView.prototype.render.apply(this,arguments);return e},close:function(){if(this.isClosed)return;this.closeRegions(),this.destroyRegions(),e.Marionette.ItemView.prototype.close.call(this,arguments)},initializeRegions:function(){this.regionManagers||(this.regionManagers={});var e=this,n=this.regions||{};t.each(n,function(t,n){var i=r.Region.buildRegion(t,e.regionType);i.getEl=function(t){return e.$(t)},e.regionManagers[n]=i,e[n]=i})},reInitializeRegions:function(){this.regionManagers&&t.size(this.regionManagers)===0?this.initializeRegions():t.each(this.regionManagers,function(e){e.reset()})},closeRegions:function(){var e=this;t.each(this.regionManagers,function(e,t){e.close()})},destroyRegions:function(){var e=this;t.each(this.regionManagers,function(t,n){delete e[n]}),this.regionManagers={}}}),r.Application=function(n){this.initCallbacks=new r.Callbacks,this.vent=new r.EventAggregator,this.commands=new e.Wreqr.Commands,this.reqres=new e.Wreqr.RequestResponse,this.submodules={},t.extend(this,n),r.addEventBinder(this)},t.extend(r.Application.prototype,e.Events,{execute:function(){this.commands.execute.apply(this.commands,arguments)},request:function(){return this.reqres.request.apply(this.reqres,arguments)},addInitializer:function(e){this.initCallbacks.add(e)},start:function(e){this.trigger("initialize:before",e),this.initCallbacks.run(e,this),this.trigger("initialize:after",e),this.trigger("start",e)},addRegions:function(e){var n=this;t.each(e,function(e,t){var i=r.Region.buildRegion(e,r.Region);n[t]=i})},removeRegion:function(e){this[e].close(),delete this[e]},module:function(e,t){var n=i.call(arguments);return n.unshift(this),r.Module.create.apply(r.Module,n)}}),r.Application.extend=e.View.extend,r.AppRouter=e.Router.extend({constructor:function(t){e.Router.prototype.constructor.call(this,t);if(this.appRoutes){var n=this.controller;t&&t.controller&&(n=t.controller),this.processAppRoutes(n,this.appRoutes)}},processAppRoutes:function(e,n){var r,i,s,o,u,a=[],f=this;for(s in n)n.hasOwnProperty(s)&&a.unshift([s,n[s]]);o=a.length;for(u=0;u<o;u++){s=a[u][0],i=a[u][1],r=e[i];if(!r){var l="Method '"+i+"' was not found on the controller",c=new Error(l);throw c.name="NoMethodError",c}r=t.bind(r,e),f.route(s,i,r)}}}),r.Module=function(e,t){this.moduleName=e,this.submodules={},this._setupInitializersAndFinalizers(),this.config={},this.config.app=t,r.addEventBinder(this)},t.extend(r.Module.prototype,e.Events,{addInitializer:function(e){this._initializerCallbacks.add(e)},addFinalizer:function(e){this._finalizerCallbacks.add(e)},start:function(e){if(this._isInitialized)return;t.each(this.submodules,function(t){t.config.options.startWithParent&&t.start(e)}),this._initializerCallbacks.run(e,this),this._isInitialized=!0},stop:function(){if(!this._isInitialized)return;this._isInitialized=!1,t.each(this.submodules,function(e){e.stop()}),this._finalizerCallbacks.run(),this._initializerCallbacks.reset(),this._finalizerCallbacks.reset()},addDefinition:function(e,t){this._runModuleDefinition(e,t)},_runModuleDefinition:function(i,s){if(!i)return;var o=t.flatten([this,this.config.app,e,r,n,t,s]);i.apply(this,o)},_setupInitializersAndFinalizers:function(){this._initializerCallbacks=new r.Callbacks,this._finalizerCallbacks=new r.Callbacks}}),t.extend(r.Module,{create:function(e,n,r){var s=this,o=e;n=n.split(".");var u=i.apply(arguments);u.splice(0,3);var a=n.length;return t.each(n,function(t,n){var i=n===a-1,f=s._getModuleDefinition(o,t,e);f.config.options=s._getModuleOptions(o,r),i&&s._configureAutoStart(e,f),i&&f.config.options.hasDefinition&&f.addDefinition(f.config.options.definition,u),o=f}),o},_configureAutoStart:function(e,t){t.config.options.startWithParent&&!t.config.autoStartConfigured&&e.addInitializer(function(e){t.start(e)}),t.config.autoStartConfigured=!0},_getModuleDefinition:function(e,t,n){var i=e[t];return i||(i=new r.Module(t,n),e[t]=i,e.submodules[t]=i),i},_getModuleOptions:function(e,n){var r={startWithParent:!0,hasDefinition:!!n};return r.hasDefinition?(t.isFunction(n)?r.definition=n:(r.hasDefinition=!!n.define,r.definition=n.define,n.hasOwnProperty("startWithParent")&&(r.startWithParent=n.startWithParent)),r):r}}),r.TemplateCache=function(e){this.templateId=e},t.extend(r.TemplateCache,{templateCaches:{},get:function(e){var t=this,n=this.templateCaches[e];return n||(n=new r.TemplateCache(e),this.templateCaches[e]=n),n.load()},clear:function(){var e,t=arguments.length;if(t>0)for(e=0;e<t;e++)delete this.templateCaches[arguments[e]];else this.templateCaches={}}}),t.extend(r.TemplateCache.prototype,{load:function(){var e=this;if(this.compiledTemplate)return this.compiledTemplate;var t=this.loadTemplate(this.templateId);return this.compiledTemplate=this.compileTemplate(t),this.compiledTemplate},loadTemplate:function(e){var t=n(e).html();if(!t||t.length===0){var r="Could not find template: '"+e+"'",i=new Error(r);throw i.name="NoTemplateError",i}return t},compileTemplate:function(e){return t.template(e)}}),r.Renderer={render:function(e,t){var n=typeof e=="function"?e:r.TemplateCache.get(e),i=n(t);return i}},r.Callbacks=function(){this._deferred=n.Deferred(),this._callbacks=[]},t.extend(r.Callbacks.prototype,{add:function(e,t){this._callbacks.push({cb:e,ctx:t}),this._deferred.done(function(n,r){t&&(n=t),e.call(n,r)})},run:function(e,t){this._deferred.resolve(t,e)},reset:function(){var e=this,r=this._callbacks;this._deferred=n.Deferred(),this._callbacks=[],t.each(r,function(t){e.add(t.cb,t.ctx)})}}),r.EventAggregator=e.Wreqr.EventAggregator;var i=Array.prototype.slice;return r}(Backbone,_,$||window.jQuery||window.Zepto||window.ender);
View
5 src/marionette.application.js
@@ -11,8 +11,9 @@ Marionette.Application = function(options){
this.reqres = new Backbone.Wreqr.RequestResponse();
this.submodules = {};
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ _.extend(this, options);
+
+ Marionette.addEventBinder(this);
};
_.extend(Marionette.Application.prototype, Backbone.Events, {
View
14 src/marionette.eventbinder.js
@@ -3,3 +3,17 @@
// Import the event binder from it's new home
// https://github.com/marionettejs/backbone.eventbinder
Marionette.EventBinder = Backbone.EventBinder;
+
+// 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.
+Marionette.addEventBinder = function(target){
+ var eventBinder = new Marionette.EventBinder();
+ target.eventBinder = eventBinder;
+ target.bindTo = _.bind(eventBinder.bindTo, eventBinder);
+ target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
+ target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
+};
View
3 src/marionette.module.js
@@ -16,8 +16,7 @@ Marionette.Module = function(moduleName, app){
this.config.app = app;
// extend this module with an event binder
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder);
+ Marionette.addEventBinder(this);
};
// Extend the Module prototype with events / bindTo, so that the module
View
3 src/marionette.region.js
@@ -10,8 +10,7 @@ Marionette.Region = function(options){
var el = this.options.el;
delete this.options.el;
- var eventBinder = new Marionette.EventBinder();
- _.extend(this, eventBinder, options);
+ Marionette.addEventBinder(this);
if (el){
this.el = el;
View
16 src/marionette.view.js
@@ -6,7 +6,7 @@ Marionette.View = Backbone.View.extend({
constructor: function(){
_.bindAll(this, "render");
- this._configureEventBinder();
+ Marionette.addEventBinder(this);
Backbone.View.prototype.constructor.apply(this, arguments);
@@ -20,20 +20,6 @@ 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 8bc76af

Please sign in to comment.