forked from marionettejs/backbone.marionette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.marionette.min.js
15 lines (15 loc) · 3.88 KB
/
backbone.marionette.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Backbone.Marionette v0.2.2
//
// 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 c={version:"0.2.2",RegionManager:function(a){this.options=a||{};if(!this.el)throw Error("An 'el' must be specified");this.el=f(this.el)}};d.extend(c.RegionManager.prototype,e.Events,{show:function(a){var b=this.currentView;this.currentView=a;this._closeView(b);this._openView(a)},_closeView:function(a){a&&a.close&&a.close()},_openView:function(a){a.render();this.el.html(a.el);if(a.onShow)a.onShow()}});c.ItemView=e.View.extend({constructor:function(){var a=h.call(arguments);
e.View.prototype.constructor.apply(this,a);this.el=f(this.el)},serializeData:function(){var a;if(this.collection)a={},a.items=this.collection.toJSON();this.model&&(a=this.model.toJSON());return a},template:function(){return f(this.options.template)},render:function(){var a=this.getTemplate(),b=this.serializeData();this.el.html(this.renderTemplate(a,b));if(this.onRender)this.onRender()},renderTemplate:function(a,b){return d.template(a.html(),b)},close:function(){this.unbindAll();this.unbind();this.remove();
if(this.onClose)this.onClose()}});c.CollectionView=e.View.extend({constructor:function(){h.call(arguments);e.View.prototype.constructor.apply(this,arguments);this.el=f(this.el);d.bindAll(this,"addChildView");this.bindTo(this.collection,"add",this.addChildView,this);this.bindTo(this.collection,"remove",this.removeChildView,this)},render:function(){this.collection.each(this.addChildView)},addChildView:function(a){this.appendHtml(this.el,this.renderItem(a))},removeChildView:function(a){var b=this.children[a.cid];
b&&(b.close(),delete this.children[a.cid])},appendHtml:function(a,b){a.append(b)},renderItem:function(a){a=new this.itemView({model:a});a.render();this.storeChild(a);return a.el},storeChild:function(a){if(!this.children)this.children={};this.children[a.model.cid]=a},close:function(){this.unbind();this.unbindAll();this.remove();this.children&&d.each(this.children,function(a){a.close()});if(this.onClose)this.onClose()}});c.BindTo={bindTo:function(a,b,c,d){d=d||this;a.bind(b,c,d);if(!this.bindings)this.bindings=
[];this.bindings.push({obj:a,eventName:b,callback:c,context:d})},unbindAll:function(){d.each(this.bindings,function(a){a.obj.unbind(a.eventName,a.callback)});this.bindings=[]}};c.Application=function(a){this.initializers=[];this.vent=d.extend({},e.Events,c.BindTo);d.extend(this,a)};d.extend(c.Application.prototype,e.Events,{addInitializer:function(a){this.initializers.push(a)},start:function(a){this.trigger("initialize:before",a);for(var b=0;b<this.initializers.length;b++)(0,this.initializers[b])(a);
this.trigger("initialize:after",a)},addRegions:function(a){if(!this.regions)this.regions={},this.addInitializer(d.bind(this._initializeRegions,this));var b=this.regions,e;for(e in a)a.hasOwnProperty(e)&&(regionValue=a[e],b[e]="string"===typeof regionValue?c.RegionManager.extend({el:regionValue}):regionValue)},_initializeRegions:function(){if(this.regions)for(var a in this.regions)this.regions.hasOwnProperty(a)&&(this[a]=new this.regions[a])}});c.TemplateManager={templates:{},get:function(a){var b=
this.templates[a];b||(b=this.loadTemplate(a),this.templates[a]=b);return b},loadTemplate:function(a){return f(a)},clear:function(){var a=arguments.length;if(1<a)for(var b=0;b<a;b++)delete this.templates[arguments[b]];else this.templates={}}};var h=Array.prototype.slice,g={getTemplate:function(){var a=this.template;return d.isFunction(a)?a.call(this):c.TemplateManager.get(a)}};d.extend(c.ItemView.prototype,g);d.extend(c.CollectionView.prototype,g);g=e.View.extend;c.RegionManager.extend=g;c.Application.extend=
g;d.extend(c.ItemView.prototype,c.BindTo);d.extend(c.CollectionView.prototype,c.BindTo);d.extend(c.Application.prototype,c.BindTo);return c}(Backbone,_,jQuery);