forked from marionettejs/backbone.marionette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.marionette.min.js
22 lines (22 loc) · 7.44 KB
/
backbone.marionette.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Backbone.Marionette v0.6.3
//
// Copyright (C)2011 Derick Bailey, Muted Solutions, LLC
// Distributed Under MIT License
//
// Documentation and Full License Available at:
// http://github.com/derickbailey/backbone.marionette
Backbone.Marionette=function(e,d,f){var b={version:"0.6.3"};b.ItemView=e.View.extend({constructor:function(){var a=k.call(arguments);e.View.prototype.constructor.apply(this,a);d.bindAll(this,"render");this.initialEvents()},initialEvents:function(){this.collection&&this.bindTo(this.collection,"reset",this.render,this)},serializeData:function(){var a;this.model?a=this.model.toJSON():this.collection&&(a={items:this.collection.toJSON()});return a},render:function(){var a=this,c=this.serializeData(),g=
f.Deferred();this.beforeRender&&this.beforeRender();this.trigger("item:before:render",a);g.done(function(){a.onRender&&a.onRender();a.trigger("item:rendered",a)});var b=this.getTemplate();f.when(b).then(function(b){b=a.renderTemplate(b,c);a.$el.html(b);g.resolve()});return g.promise()},renderTemplate:function(a,c){if(!a||0===a.length){var g=Error("A template must be specified");g.name="NoTemplateError";throw g;}return d.template(a,c)},getTemplate:function(){var a=this.template||this.options.template;
d.isFunction(a)&&(a=a.call(this));return b.TemplateCache.get(a)},close:function(){this.trigger("item:before:close");this.beforeClose&&this.beforeClose();this.unbindAll();this.unbind();this.remove();this.onClose&&this.onClose();this.trigger("item:closed")}});b.CollectionView=e.View.extend({constructor:function(){e.View.prototype.constructor.apply(this,arguments);d.bindAll(this,"addChildView","render");this.initialEvents()},initialEvents:function(){this.bindTo(this.collection,"add",this.addChildView,
this);this.bindTo(this.collection,"remove",this.removeChildView,this);this.bindTo(this.collection,"reset",this.render,this)},render:function(){var a=this,c=f.Deferred(),g=[];this.beforeRender&&this.beforeRender();this.trigger("collection:before:render",this);this.closeChildren();if(!this.itemView){var b=Error("An `itemView` must be specified");b.name="NoItemViewError";throw b;}this.collection.each(function(c){c=a.addChildView(c);g.push(c)});c.done(function(){this.onRender&&this.onRender();this.trigger("collection:rendered",
this)});f.when(g).then(function(){c.resolveWith(a)});return c.promise()},addChildView:function(a){var c=this,b=new this.itemView({model:a});this.storeChild(b);a=b.render();f.when(a).then(function(){c.appendHtml(c.$el,b.$el)});return a},removeChildView:function(a){var c=this.children[a.cid];c&&(c.close(),delete this.children[a.cid])},appendHtml:function(a,c){a.append(c)},storeChild:function(a){this.children||(this.children={});this.children[a.model.cid]=a},close:function(){this.beforeClose&&this.beforeClose();
this.trigger("collection:before:close");this.unbind();this.unbindAll();this.closeChildren();this.remove();this.onClose&&this.onClose();this.trigger("collection:closed")},closeChildren:function(){this.children&&d.each(this.children,function(a){a.close()})}});b.CompositeView=b.CollectionView.extend({modelView:b.ItemView,render:function(){var a=this,c=f.Deferred();this.renderedModelView=new this.modelView({model:this.model,template:this.template});var b=this.renderModel();f.when(b).then(function(){a.$el.html(a.renderedModelView.el);
a.trigger("composite:model:rendered");var b=a.renderCollection();f.when(b).then(function(){c.resolve()})});c.done(function(){a.trigger("composite:rendered")});return c.promise()},renderCollection:function(){var a=b.CollectionView.prototype.render.apply(this,arguments);a.done(function(){this.trigger("composite:collection:rendered")});return a.promise()},renderModel:function(){if(this.renderedModelView)return this.renderedModelView.render()},close:function(){b.CollectionView.prototype.close.apply(this,
arguments);this.renderedModelView&&(this.renderedModelView.close&&this.renderedModelView.close(),delete this.renderedModelView)}});b.Region=function(a){this.options=a||{};d.extend(this,a);if(!this.el)throw a=Error("An 'el' must be specified"),a.name="NoElError",a;};d.extend(b.Region.prototype,e.Events,{show:function(a,c){this.ensureEl();this.close();this.open(a,c);this.currentView=a},ensureEl:function(){if(!this.$el||0==this.$el.length)this.$el=this.getEl(this.el)},getEl:function(a){return f(a)},
open:function(a,c){var b=this,c=c||"html";f.when(a.render()).then(function(){b.$el[c](a.el);a.onShow&&a.onShow();b.trigger("view:show",a)})},close:function(){var a=this.currentView;a&&(a.close&&a.close(),this.trigger("view:closed",a),delete this.currentView)},attachView:function(a){this.currentView=a}});b.Layout=b.ItemView.extend({constructor:function(){this.vent=new e.Marionette.EventAggregator;e.Marionette.ItemView.apply(this,arguments);this.regionManagers={}},render:function(){this.initializeRegions();
return e.Marionette.ItemView.prototype.render.call(this,arguments)},close:function(){this.closeRegions();e.Marionette.ItemView.prototype.close.call(this,arguments)},initializeRegions:function(){var a=this;d.each(this.regions,function(c,b){var d=new e.Marionette.Region({el:c,getEl:function(c){return a.$(c)}});a.regionManagers[b]=d;a[b]=d})},closeRegions:function(){var a=this;d.each(this.regionManagers,function(c,b){c.close();delete a[b]});this.regionManagers={}}});b.AppRouter=e.Router.extend({constructor:function(a){e.Router.prototype.constructor.call(this,
a);if(this.appRoutes){var c=this.controller;a&&a.controller&&(c=a.controller);this.processAppRoutes(c,this.appRoutes)}},processAppRoutes:function(a,c){var b,e,f,h,i=[];for(f in c)i.unshift([f,c[f]]);h=i.length;for(var j=0;j<h;j++)f=i[j][0],e=i[j][1],b=d.bind(a[e],a),this.route(f,e,b)}});b.BindTo={bindTo:function(a,c,b,d){d=d||this;a.on(c,b,d);this.bindings||(this.bindings=[]);this.bindings.push({obj:a,eventName:c,callback:b,context:d})},unbindAll:function(){d.each(this.bindings,function(a){a.obj.off(a.eventName,
a.callback)});this.bindings=[]}};b.Callbacks=function(){this.deferred=f.Deferred();this.promise=this.deferred.promise()};d.extend(b.Callbacks.prototype,{add:function(a){this.promise.done(function(b,d){a.call(b,d)})},run:function(a,b){this.deferred.resolve(a,b)}});b.EventAggregator=function(a){d.extend(this,a)};d.extend(b.EventAggregator.prototype,e.Events,b.BindTo,{bindTo:function(a,c,d){b.BindTo.bindTo.call(this,this,a,c,d)}});b.Application=function(a){this.initCallbacks=new b.Callbacks;this.vent=
new b.EventAggregator;d.extend(this,a)};d.extend(b.Application.prototype,e.Events,{addInitializer:function(a){this.initCallbacks.add(a)},start:function(a){this.trigger("initialize:before",a);this.initCallbacks.run(this,a);this.trigger("initialize:after",a);this.trigger("start",a)},addRegions:function(a){var c,d;for(d in a)a.hasOwnProperty(d)&&(c=a[d],c="string"===typeof c?new b.Region({el:c}):new c,this[d]=c)}});b.TemplateCache={templates:{},loaders:{},get:function(a){var b=this,d=f.Deferred(),e=
this.templates[a];e?d.resolve(e):(e=this.loaders[a])?d=e:(this.loaders[a]=d,this.loadTemplate(a,function(e){delete b.loaders[a];b.templates[a]=e;d.resolve(e)}));return d.promise()},loadTemplate:function(a,b){var d=f(a).html();b.call(this,d)},clear:function(){var a=arguments.length;if(0<a)for(var b=0;b<a;b++)delete this.templates[arguments[b]];else this.templates={}}};var k=Array.prototype.slice,h=e.View.extend;b.Region.extend=h;b.Application.extend=h;d.extend(b.ItemView.prototype,b.BindTo);d.extend(b.CollectionView.prototype,
b.BindTo);d.extend(b.Application.prototype,b.BindTo);d.extend(b.Region.prototype,b.BindTo);return b}(Backbone,_,window.jQuery||window.Zepto||window.ender);