/*! * Backbone.Marionette, v1.0.0-beta4 * Copyright (c)2012 Derick Bailey, Muted Solutions, LLC. * Distributed under MIT license * http://github.com/marionettejs/backbone.marionette */ /*! * Includes Wreqr * https://github.com/marionettejs/backbone.wreqr/ * Includes EventBinder * https://github.com/marionettejs/backbone.eventbinder/ */ Backbone.EventBinder=function(e,t){"use strict";function r(e){return e.jquery?n.jquery:n["default"]}var n={"default":{bindTo:function(e,t,n,r){r=r||this,e.on(t,n,r);var i={type:"default",obj:e,eventName:t,callback:n,context:r};return i},unbindFrom:function(e){e.obj.off(e.eventName,e.callback,e.context)}},jquery:{bindTo:function(e,n,r,i){i=i||this,r=t(r).bind(i),e.on(n,r);var s={type:"jquery",obj:e,eventName:n,callback:r,context:i};return s},unbindFrom:function(e){e.obj.off(e.eventName,e.callback)}}},i=function(){this._eventBindings=[]};return i.extend=e.View.extend,t.extend(i.prototype,{bindTo:function(){var e=arguments[0],t=r(e),n=t.bindTo.apply(this,arguments);return this._eventBindings.push(n),n},unbindFrom:function(e){n[e.type].unbindFrom.apply(this,arguments),this._eventBindings=t.reject(this._eventBindings,function(t){return t===e})},unbindAll:function(){var e=t.map(this._eventBindings,t.identity);t.each(e,this.unbindFrom,this)}}),i}(Backbone,_),Backbone.Wreqr=function(e,t,n){"option strict";var r={};return r.Handlers=function(e,t){var n=function(){"use strict";this._handlers={}};return n.extend=e.Model.extend,t.extend(n.prototype,{addHandler:function(e,t,n){var r={callback:t,context:n};this._handlers[e]=r},getHandler:function(e){var t=this._handlers[e];if(!t)throw new Error("Handler not found for '"+e+"'");return function(){return t.callback.apply(t.context,arguments)}},removeHandler:function(e){delete this._handlers[e]},removeAllHandlers:function(){this._handlers={}}}),n}(e,n),r.Commands=function(e){return e.Handlers.extend({execute:function(e,t){this.getHandler(e)(t)}})}(r),r.RequestResponse=function(e){return e.Handlers.extend({request:function(e,t){return this.getHandler(e)(t)}})}(r),r.EventAggregator=function(e,t){var n=function(){};return n.extend=e.Model.extend,t.extend(n.prototype,e.Events),n}(e,n),r}(Backbone,Backbone.Marionette,_),Backbone.Marionette=Marionette=function(e,t,n){var r={},i=Array.prototype.slice;return r.extend=e.Model.extend,r.getOption=function(e,t){if(!e||!t)return;var n;return e.options&&e.options[t]?n=e.options[t]:n=e[t],n},r.createObject=function(){function t(){}var e;return typeof Object.create=="function"?e=Object.create:e=function(e){t.prototype=e;var n=new t;return t.prototype=null,n},e}(),r.triggerMethod=function(){var e=Array.prototype.slice.apply(arguments),n=e[0],r=n.split(":"),i,s,o="on";for(var u=0;u0)for(e=0;e0?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=r.getOption(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=r.getOption(this,"itemView");if(!t){var n=new Error("An `itemView` must be specified");throw n.name="NoItemViewError",n}return t},addItemView:function(e,n,r){var s=this,o;t.isFunction(this.itemViewOptions)?o=this.itemViewOptions(e):o=this.itemViewOptions;var u=this.buildItemView(e,n,o);this.storeChild(e,u),this.triggerMethod("item:added",u);var a=this.bindTo(u,"all",function(){var e=i.call(arguments);e[0]="itemview:"+e[0],e.splice(1,0,u),s.triggerMethod.apply(s,e)});this.childBindings=this.childBindings||{},this.childBindings[u.cid]=a;var f=this.renderItemView(u,r);return u.onShow&&this.onShowCallbacks.add(u.onShow,u),f},renderItemView:function(e,t){e.render(),this.appendHtml(this,e,t)},buildItemView:function(e,n,r){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,t){this.children[e.cid]=t},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=r.getOption(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},render:function(){this.isClosed=!1,this.resetItemViewContainer();var e=this.renderModel();return this.$el.html(e),this.bindUIElements(),this.triggerMethod("composite:model:rendered"),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(),e=this.mixinTemplateHelpers(e);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.Layout=r.ItemView.extend({regionType:r.Region,constructor:function(){this._firstRender=!0,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.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