-
Notifications
You must be signed in to change notification settings - Fork 6
/
Marionette.BossView.AMD.min.js
5 lines (5 loc) · 3.92 KB
/
Marionette.BossView.AMD.min.js
1
2
3
4
5
/*
* https://github.com/justspamjustin/BossView
* BossView v 0.1.4
*/
define(function(e){"use strict";var t=e("underscore");var n=e("marionette");n.BossView=n.ItemView.extend({template:function(){return""},constructor:function(){n.ItemView.prototype.constructor.apply(this,arguments);this._initializeSubViews();this._afterInitializeSubViews();this.listenTo(this,"render",this._onParentRendered)},getParentEl:function(){return this.$el},_afterInitializeSubViews:function(){this._initializeChildViewEvents();this._initializeSubViewEventBubbling()},_initializeSubViews:function(){this.initializedSubViews={};this._eachSubView(t.bind(this._initializeSubView,this))},_initializeSubView:function(e,t){var n=this._getInitializedSubView(t);this._checkSubViewForRender(n,e);this[e]=n;this.initializedSubViews[e]=n},initializeSubView:function(e,t){this._initializeSubView(e,t);this._afterInitializeSubViews()},_getInitializedSubView:function(e){var n;var r=t.isFunction(e.prototype.render);if(r){n=this._initializeRenderableSubView(e)}else{n=e.call(this)}return n},_initializeRenderableSubView:function(e){return new e({model:this.model,collection:this.collection})},_checkSubViewForRender:function(e,n){if(t.isUndefined(e)||!t.isFunction(e.render)){throw new Error("The subview named "+n+" does not have a render function.")}},_initializeChildViewEvents:function(){this._eachSubViewEvent(t.bind(function(e,t,n){n=this._getSubViewEventCallbackFunction(n,t);if(e==="*"){this._listenToEventOnAllSubViews(n,t)}else{if(e){this.listenTo(e,t,n)}}},this))},_getSubViewEventCallbackFunction:function(e,n){if(t.isString(e)){this._checkForSubViewEventCallback(e,n);e=this[e]}return e},_listenToEventOnAllSubViews:function(e,n){this._eachSubView(t.bind(function(t){var r=this[t];this.listenTo(r,n,e)},this))},_checkForSubViewEventCallback:function(e,n){if(t.isUndefined(this[e])){throw new Error("This view has no function named "+e+" to use as a callback for the event "+n)}},_initializeSubViewEventBubbling:function(){this._eachSubView(t.bind(function(e){var t=this[e];this.listenTo(t,"all",function(){this.trigger(e+":"+arguments[0],arguments[1])})},this))},_onParentRendered:function(){this.trigger("subviews:before:render");this._renderSubViews();this.trigger("subviews:after:render")},_renderSubViews:function(){this._eachSubView(t.bind(this.renderSubView,this))},renderSubView:function(e){var t=this._getOption("mainSubViewContainer");var n=this.getParentEl();if(this._hasSubViewContainer(e)){n=this._getSubViewContainer(e)}else if(t){n=this.$(t)}this._renderSubView(e,n)},_renderSubView:function(e,t){if(this._shouldRenderSubView(e)){this[e].render().$el.appendTo(t);this[e].delegateEvents()}},_shouldRenderSubView:function(e){var n=this._getSubViewRenderConditions()[e];var r=t.isFunction(n);return r?n.call(this):true},_eachSubView:function(e){if(this._getSubViews()){for(var t in this._getSubViews()){e(t,this._getSubViews()[t])}}},_eachSubViewEvent:function(e){var t=this._getOption("subViewEvents");if(t){for(var n in t){var r=this._splitSubViewEventKey(n);var i=r.subViewName==="*"?"*":this[r.subViewName];e(i,r.subViewEventName,t[n])}}},_splitSubViewEventKey:function(e){var t=e.split(" ");return{subViewName:t[0],subViewEventName:t[1]}},_hasSubViewContainer:function(e){var n=this._getOption("subViewContainers");return!t.isUndefined(n)&&!t.isUndefined(n[e])},_getSubViewContainer:function(e){if(!this._hasSubViewContainer(e)){throw new Error("No subview container for subView: "+e)}return this.$(this._getOption("subViewContainers")[e])},remove:function(){n.ItemView.prototype.remove.apply(this,arguments);this._removeSubViews()},_removeSubViews:function(){t.each(this.initializedSubViews,function(e){e.remove()})},_getSubViews:function(){var e=t.result(this,"subViews");if(this.options.subViews){e=t.result(this.options,"subViews")}return e},_getOption:function(e){return this[e]||this.options[e]},_getSubViewRenderConditions:function(){return this._getOption("subViewRenderConditions")||{}}});return n.BossView})