marionette.toolkit
Advanced tools
Comparing version 2.0.0 to 3.0.0
{ | ||
"name": "marionette.toolkit", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A collection of opinionated Backbone.Marionette extensions for large scale application architecture.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/marionette.toolkit.js", |
@@ -0,1 +1,12 @@ | ||
#### v3.0.0 | ||
* `App` | ||
* Add `setRegion` to modify an App's region | ||
* All `region` to be passed to `App#start` | ||
* `start` and `stop` are no longer binded to the App | ||
* the stop event is triggered after clearing running events | ||
* General | ||
* Support lodash v4 | ||
* Fixed an underscore import | ||
#### v2.0.0 | ||
@@ -2,0 +13,0 @@ |
/** | ||
* marionette.toolkit - A collection of opinionated Backbone.Marionette extensions for large scale application architecture. | ||
* @version v2.0.0 | ||
* @version v3.0.0 | ||
* @link https://github.com/RoundingWellOS/marionette.toolkit | ||
@@ -9,9 +9,9 @@ * @license MIT | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('backbone.marionette'), require('underscore'), require('backbone')) : | ||
typeof define === 'function' && define.amd ? define(['backbone.marionette', 'underscore', 'backbone'], factory) : | ||
(global.Marionette = global.Marionette || {}, global.Marionette.Toolkit = factory(global.Marionette,global._,global.Backbone)); | ||
}(this, function (Marionette,_$1,Backbone) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('backbone.marionette'), require('backbone')) : | ||
typeof define === 'function' && define.amd ? define(['underscore', 'backbone.marionette', 'backbone'], factory) : | ||
(global.Marionette = global.Marionette || {}, global.Marionette.Toolkit = factory(global._,global.Marionette,global.Backbone)); | ||
}(this, function (_,Marionette,Backbone) { 'use strict'; | ||
_ = 'default' in _ ? _['default'] : _; | ||
Marionette = 'default' in Marionette ? Marionette['default'] : Marionette; | ||
_$1 = 'default' in _$1 ? _$1['default'] : _$1; | ||
Backbone = 'default' in Backbone ? Backbone['default'] : Backbone; | ||
@@ -44,3 +44,3 @@ | ||
initState: function initState() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -87,3 +87,3 @@ // Make defaults available to this | ||
_setEventHandlers: function _setEventHandlers() { | ||
this.bindEvents(this._stateModel, _$1.result(this, 'stateEvents')); | ||
this.bindEvents(this._stateModel, _.result(this, 'stateEvents')); | ||
@@ -107,3 +107,3 @@ this.on('destroy', this._destroyState); | ||
return this.StateModel; | ||
} else if (_$1.isFunction(this.StateModel)) { | ||
} else if (_.isFunction(this.StateModel)) { | ||
return this.StateModel.call(this, options); | ||
@@ -143,3 +143,3 @@ } | ||
resetStateDefaults: function resetStateDefaults() { | ||
var defaults = _$1.result(this._stateModel, 'defaults'); | ||
var defaults = _.result(this._stateModel, 'defaults'); | ||
@@ -208,3 +208,3 @@ return this._stateModel.set(defaults); | ||
_initChildApps: function _initChildApps() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -218,3 +218,3 @@ this._childApps = {}; | ||
if (childApps) { | ||
if (_$1.isFunction(childApps)) { | ||
if (_.isFunction(childApps)) { | ||
childApps = childApps.call(this, options); | ||
@@ -253,4 +253,4 @@ } | ||
_startChildApps: function _startChildApps() { | ||
_$1.each(this._childApps, function (childApp) { | ||
if (_$1.result(childApp, 'startWithParent')) { | ||
_.each(this._childApps, function (childApp) { | ||
if (_.result(childApp, 'startWithParent')) { | ||
childApp.start(); | ||
@@ -269,4 +269,4 @@ } | ||
_stopChildApps: function _stopChildApps() { | ||
_$1.each(this._childApps, function (childApp) { | ||
if (_$1.result(childApp, 'stopWithParent')) { | ||
_.each(this._childApps, function (childApp) { | ||
if (_.result(childApp, 'stopWithParent')) { | ||
childApp.stop(); | ||
@@ -282,3 +282,3 @@ } | ||
* @param {String} appName - Name of childApp to start | ||
* @param {object} options - Start options for app | ||
* @param {Object} options - Start options for app | ||
* @public | ||
@@ -315,4 +315,4 @@ * @method startChildApp | ||
_destroyChildApps: function _destroyChildApps() { | ||
_$1.each(this._childApps, function (childApp) { | ||
if (!_$1.result(childApp, 'preventDestroy')) { | ||
_.each(this._childApps, function (childApp) { | ||
if (!_.result(childApp, 'preventDestroy')) { | ||
childApp.destroy(); | ||
@@ -334,3 +334,3 @@ } | ||
var AppClass = appConfig.AppClass; | ||
var options = _$1.omit(appConfig, 'AppClass'); | ||
var options = _.omit(appConfig, 'AppClass'); | ||
@@ -352,6 +352,6 @@ return this.buildApp(AppClass, options); | ||
_buildApp: function _buildApp(AppClass, options) { | ||
if (_$1.isFunction(AppClass)) { | ||
if (_.isFunction(AppClass)) { | ||
return this.buildApp(AppClass, options); | ||
} | ||
if (_$1.isObject(AppClass)) { | ||
if (_.isObject(AppClass)) { | ||
return this._buildAppFromObject(AppClass); | ||
@@ -374,3 +374,3 @@ } | ||
// options on childApp definition supersede childAppOptions | ||
options = _$1.extend({}, this.childAppOptions, options); | ||
options = _.extend({}, this.childAppOptions, options); | ||
@@ -407,5 +407,5 @@ return new AppClass(options); | ||
addChildApps: function addChildApps(childApps) { | ||
_$1.each(childApps, function (childApp, appName) { | ||
_.each(childApps, _.bind(function (childApp, appName) { | ||
this.addChildApp(appName, childApp); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -444,5 +444,5 @@ | ||
// When the app is destroyed remove the cached app. | ||
childApp.on('destroy', _$1.partial(this._removeChildApp, appName), this); | ||
childApp.on('destroy', _.partial(this._removeChildApp, appName), this); | ||
if (this.isRunning() && _$1.result(childApp, 'startWithParent')) { | ||
if (this.isRunning() && _.result(childApp, 'startWithParent')) { | ||
childApp.start(); | ||
@@ -475,3 +475,3 @@ } | ||
getChildApps: function getChildApps() { | ||
return _$1.clone(this._childApps); | ||
return _.clone(this._childApps); | ||
}, | ||
@@ -518,5 +518,5 @@ | ||
_$1.each(this._childApps, function (childApp, appName) { | ||
_.each(this._childApps, _.bind(function (childApp, appName) { | ||
this.removeChildApp(appName); | ||
}, this); | ||
}, this)); | ||
@@ -538,3 +538,3 @@ return childApps; | ||
removeChildApp: function removeChildApp(appName, options) { | ||
options = _$1.extend({}, options); | ||
options = _.extend({}, options); | ||
@@ -548,3 +548,3 @@ var childApp = this.getChildApp(appName); | ||
// if preventDestroy simply unregister the child app | ||
if (options.preventDestroy || _$1.result(childApp, 'preventDestroy')) { | ||
if (options.preventDestroy || _.result(childApp, 'preventDestroy')) { | ||
this._removeChildApp(appName); | ||
@@ -574,5 +574,5 @@ } else { | ||
_stopRunningEvents: function _stopRunningEvents() { | ||
_$1.each(this._runningEvents, function (args) { | ||
_.each(this._runningEvents, _.bind(function (args) { | ||
this.off.apply(this, args); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -588,5 +588,5 @@ | ||
_stopRunningListeners: function _stopRunningListeners() { | ||
_$1.each(this._runningListeningTo, function (args) { | ||
_.each(this._runningListeningTo, _.bind(function (args) { | ||
this.stopListening.apply(this, args); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -712,6 +712,4 @@ | ||
constructor: function constructor() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_$1.bindAll(this, 'start', 'stop'); | ||
this.mergeOptions(options, ClassOptions$1); | ||
@@ -723,3 +721,3 @@ | ||
if (_$1.result(this, 'startAfterInitialized')) { | ||
if (_.result(this, 'startAfterInitialized')) { | ||
this.start(options); | ||
@@ -778,5 +776,8 @@ } | ||
var opts = _.extend({}, options); | ||
this.setRegion(opts.region); | ||
this.triggerMethod('before:start', options); | ||
var opts = _$1.extend({}, options); | ||
opts.state = this.getInitState(opts.state); | ||
@@ -795,2 +796,23 @@ | ||
/** | ||
* Set the Application's Region after instantiation | ||
* | ||
* @public | ||
* @method setRegion | ||
* @memberOf App | ||
* @param {Region} [region] - Region to use with the app | ||
* @returns {App} | ||
*/ | ||
setRegion: function setRegion(region) { | ||
if (!region) { | ||
return this; | ||
} | ||
this._region = region; | ||
return this; | ||
}, | ||
/** | ||
* Returns state. | ||
@@ -848,7 +870,7 @@ * Override to extend state | ||
this.triggerMethod('stop', options); | ||
this._stopRunningListeners(); | ||
this._stopRunningEvents(); | ||
this.triggerMethod('stop', options); | ||
return this; | ||
@@ -876,3 +898,3 @@ }, | ||
_$1.extend(App.prototype, StateMixin, ChildAppsMixin, EventListenersMixin); | ||
_.extend(App.prototype, StateMixin, ChildAppsMixin, EventListenersMixin); | ||
@@ -925,3 +947,3 @@ var ClassOptions$3 = ['ViewClass', 'viewEventPrefix', 'viewOptions', 'region']; | ||
constructor: function constructor() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -1036,3 +1058,3 @@ // Make defaults available to this | ||
_getViewClass: function _getViewClass() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -1043,3 +1065,3 @@ var ViewClass = this.ViewClass; | ||
return ViewClass; | ||
} else if (_$1.isFunction(ViewClass)) { | ||
} else if (_.isFunction(ViewClass)) { | ||
return ViewClass.call(this, options); | ||
@@ -1110,3 +1132,3 @@ } | ||
view.on('all', function () { | ||
var args = _$1.toArray(arguments); | ||
var args = _.toArray(arguments); | ||
var rootEvent = args[0]; | ||
@@ -1133,5 +1155,5 @@ | ||
mixinOptions: function mixinOptions(options) { | ||
var viewOptions = _$1.result(this, 'viewOptions'); | ||
var viewOptions = _.result(this, 'viewOptions'); | ||
return _$1.extend({ state: this.getState().attributes }, viewOptions, options); | ||
return _.extend({ state: this.getState().attributes }, viewOptions, options); | ||
}, | ||
@@ -1207,3 +1229,3 @@ | ||
_$1.extend(Component.prototype, StateMixin); | ||
_.extend(Component.prototype, StateMixin); | ||
@@ -1233,3 +1255,3 @@ /** | ||
Toolkit.VERSION = '2.0.0'; | ||
Toolkit.VERSION = '3.0.0'; | ||
@@ -1236,0 +1258,0 @@ Toolkit.StateMixin = StateMixin; |
/** | ||
* marionette.toolkit - A collection of opinionated Backbone.Marionette extensions for large scale application architecture. | ||
* @version v2.0.0 | ||
* @version v3.0.0 | ||
* @link https://github.com/RoundingWellOS/marionette.toolkit | ||
* @license MIT | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("backbone.marionette"),require("underscore"),require("backbone")):"function"==typeof define&&define.amd?define(["backbone.marionette","underscore","backbone"],e):(t.Marionette=t.Marionette||{},t.Marionette.Toolkit=e(t.Marionette,t._,t.Backbone))}(this,function(t,e,i){"use strict";t="default"in t?t.default:t,e="default"in e?e.default:e,i="default"in i?i.default:i;var n=["StateModel","stateEvents"],s={StateModel:i.Model,initState:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];this.mergeOptions(t,n),this._removeEventHandlers();var e=this._getStateModel(t);return this._stateModel=new e(t.state),this._setEventHandlers(),this},_removeEventHandlers:function(){this._stateModel&&(this.unbindEvents(this._stateModel),this._stateModel.stopListening(),this.off("destroy",this._destroyState))},_setEventHandlers:function(){this.bindEvents(this._stateModel,e.result(this,"stateEvents")),this.on("destroy",this._destroyState)},_getStateModel:function(n){if(this.StateModel.prototype instanceof i.Model||this.StateModel===i.Model)return this.StateModel;if(e.isFunction(this.StateModel))return this.StateModel.call(this,n);throw new t.Error({name:"InvalidStateModelError",message:'"StateModel" must be a model class or a function that returns a model class'})},setState:function(){return this._stateModel.set.apply(this._stateModel,arguments)},resetStateDefaults:function(){var t=e.result(this._stateModel,"defaults");return this._stateModel.set(t)},getState:function(t){return t?this._stateModel.get.apply(this._stateModel,arguments):this._stateModel},_destroyState:function(){this._stateModel.stopListening()}},r=["childApps","childAppOptions"],o={_initChildApps:function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];this._childApps={},this.mergeOptions(t,r);var i=this.childApps;i&&(e.isFunction(i)&&(i=i.call(this,t)),this.addChildApps(i)),this._initListeners()},_initListeners:function(){this.on({start:this._startChildApps,"before:stop":this._stopChildApps,"before:destroy":this._destroyChildApps})},_startChildApps:function(){e.each(this._childApps,function(t){e.result(t,"startWithParent")&&t.start()})},_stopChildApps:function(){e.each(this._childApps,function(t){e.result(t,"stopWithParent")&&t.stop()})},startChildApp:function(t,e){return this.getChildApp(t).start(e),this},stopChildApp:function(t){return this.getChildApp(t).stop(),this},_destroyChildApps:function(){e.each(this._childApps,function(t){e.result(t,"preventDestroy")||t.destroy()})},_buildAppFromObject:function(t){var i=t.AppClass,n=e.omit(t,"AppClass");return this.buildApp(i,n)},_buildApp:function(t,i){return e.isFunction(t)?this.buildApp(t,i):e.isObject(t)?this._buildAppFromObject(t):void 0},buildApp:function(t,i){return i=e.extend({},this.childAppOptions,i),new t(i)},_ensureAppIsUnique:function(e){if(this._childApps[e])throw new t.Error({name:"DuplicateChildAppError",message:'A child App with name "'+e+'" has already been added.'})},addChildApps:function(t){e.each(t,function(t,e){this.addChildApp(e,t)},this)},addChildApp:function(i,n,s){this._ensureAppIsUnique(i);var r=this._buildApp(n,s);if(!r)throw new t.Error({name:"AddChildAppError",message:"App build failed. Incorrect configuration."});return r._name=i,this._childApps[i]=r,r.on("destroy",e.partial(this._removeChildApp,i),this),this.isRunning()&&e.result(r,"startWithParent")&&r.start(),r},getName:function(){return this._name},getChildApps:function(){return e.clone(this._childApps)},getChildApp:function(t){return this._childApps[t]},_removeChildApp:function(t){delete this._childApps[t]._name,delete this._childApps[t]},removeChildApps:function(){var t=this.getChildApps();return e.each(this._childApps,function(t,e){this.removeChildApp(e)},this),t},removeChildApp:function(t,i){i=e.extend({},i);var n=this.getChildApp(t);if(n)return i.preventDestroy||e.result(n,"preventDestroy")?this._removeChildApp(t):n.destroy(),n}},h={_stopRunningEvents:function(){e.each(this._runningEvents,function(t){this.off.apply(this,t)},this)},_stopRunningListeners:function(){e.each(this._runningListeningTo,function(t){this.stopListening.apply(this,t)},this)},on:function(){return this._isRunning&&(this._runningEvents=this._runningEvents||[],this._runningEvents.push(arguments)),t.Object.prototype.on.apply(this,arguments)},listenTo:function(){return this._isRunning&&(this._runningListeningTo=this._runningListeningTo||[],this._runningListeningTo.push(arguments)),t.Object.prototype.listenTo.apply(this,arguments)},listenToOnce:function(){return this._isRunning&&(this._runningListeningTo=this._runningListeningTo||[],this._runningListeningTo.push(arguments)),t.Object.prototype.listenToOnce.apply(this,arguments)}},p=["startWithParent","stopWithParent","startAfterInitialized","preventDestroy","StateModel"],a=t.Application.extend({_isRunning:!1,preventDestroy:!1,startAfterInitialized:!1,startWithParent:!1,stopWithParent:!0,constructor:function(){var i=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];e.bindAll(this,"start","stop"),this.mergeOptions(i,p),this._initChildApps(i),t.Application.call(this,i),e.result(this,"startAfterInitialized")&&this.start(i)},_ensureAppIsIntact:function(){if(this._isDestroyed)throw new t.Error({name:"AppDestroyedError",message:"App has already been destroyed and cannot be used."})},isRunning:function(){return this._isRunning},start:function(t){if(this._ensureAppIsIntact(),this._isRunning)return this;this.triggerMethod("before:start",t);var i=e.extend({},t);return i.state=this.getInitState(i.state),this.initState(i),this._isRunning=!0,this.triggerStart(i),this},getInitState:function(t){return t},triggerStart:function(t){this.triggerMethod("start",t)},stop:function(t){return this._isRunning?(this.triggerMethod("before:stop",t),this._isRunning=!1,this.triggerMethod("stop",t),this._stopRunningListeners(),this._stopRunningEvents(),this):this},destroy:function(){this._isDestroyed||(this.stop(),t.Object.prototype.destroy.apply(this,arguments))}});e.extend(a.prototype,s,o,h);var d=["ViewClass","viewEventPrefix","viewOptions","region"],u=t.Object.extend({ViewClass:t.View,viewEventPrefix:"view",viewOptions:{},constructor:function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];this.mergeOptions(e,d),this.initState(e),t.Object.call(this,e)},_shouldDestroy:!0,showIn:function(t,e){return this.region=t,this.show(e),this},show:function(e){var i=this.getRegion();if(this._isShown)throw new t.Error({name:"ComponentShowError",message:"Component has already been shown in a region."});if(!i)throw new t.Error({name:"ComponentRegionError",message:"Component has no defined region."});return this.triggerMethod("before:show"),this.renderView(e),this._isShown=!0,this.triggerMethod("show"),this.listenTo(i,"empty",this._destroy),this},getRegion:function(){return this.region},_getViewClass:function(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],s=this.ViewClass;if(s.prototype instanceof i.View||s===i.View)return s;if(e.isFunction(s))return s.call(this,n);throw new t.Error({name:"InvalidViewClassError",message:'"ViewClass" must be a view class or a function that returns a view class'})},renderView:function(t){var e=this._getViewClass(t),i=this.mixinOptions(t),n=this.buildView(e,i);return this.currentView=n,this._proxyViewEvents(n),this.triggerMethod("before:render:view",n),this._shouldDestroy=!1,this.getRegion().show(n),this._shouldDestroy=!0,this.triggerMethod("render:view",n),this},_proxyViewEvents:function(t){var i=this.viewEventPrefix;t.on("all",function(){var n=e.toArray(arguments),s=n[0];n[0]=i+":"+s,n.splice(1,0,t),this.triggerMethod.apply(this,n)},this)},mixinOptions:function(t){var i=e.result(this,"viewOptions");return e.extend({state:this.getState().attributes},i,t)},buildView:function(t,e){return new t(e)},_destroy:function(){this._shouldDestroy&&t.Object.prototype.destroy.apply(this,arguments)},_emptyRegion:function(t){var e=this.getRegion();e&&(this.stopListening(e,"empty"),e.empty(t))},destroy:function(t){this._emptyRegion(t),this._shouldDestroy=!0,this._destroy(t)}});e.extend(u.prototype,s);var l=t.Toolkit,c=t.Toolkit={};return c.noConflict=function(){return t.Toolkit=l,this},c.MixinState=function(t){var e=s;t.prototype.StateModel&&(e=_.omit(s,"StateModel")),_.extend(t.prototype,e)},c.VERSION="2.0.0",c.StateMixin=s,c.App=a,c.Component=u,c}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("underscore"),require("backbone.marionette"),require("backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone.marionette","backbone"],e):(t.Marionette=t.Marionette||{},t.Marionette.Toolkit=e(t._,t.Marionette,t.Backbone))}(this,function(t,e,i){"use strict";t="default"in t?t.default:t,e="default"in e?e.default:e,i="default"in i?i.default:i;var n=["StateModel","stateEvents"],s={StateModel:i.Model,initState:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.mergeOptions(t,n),this._removeEventHandlers();var e=this._getStateModel(t);return this._stateModel=new e(t.state),this._setEventHandlers(),this},_removeEventHandlers:function(){this._stateModel&&(this.unbindEvents(this._stateModel),this._stateModel.stopListening(),this.off("destroy",this._destroyState))},_setEventHandlers:function(){this.bindEvents(this._stateModel,t.result(this,"stateEvents")),this.on("destroy",this._destroyState)},_getStateModel:function(n){if(this.StateModel.prototype instanceof i.Model||this.StateModel===i.Model)return this.StateModel;if(t.isFunction(this.StateModel))return this.StateModel.call(this,n);throw new e.Error({name:"InvalidStateModelError",message:'"StateModel" must be a model class or a function that returns a model class'})},setState:function(){return this._stateModel.set.apply(this._stateModel,arguments)},resetStateDefaults:function(){var e=t.result(this._stateModel,"defaults");return this._stateModel.set(e)},getState:function(t){return t?this._stateModel.get.apply(this._stateModel,arguments):this._stateModel},_destroyState:function(){this._stateModel.stopListening()}},r=["childApps","childAppOptions"],o={_initChildApps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this._childApps={},this.mergeOptions(e,r);var i=this.childApps;i&&(t.isFunction(i)&&(i=i.call(this,e)),this.addChildApps(i)),this._initListeners()},_initListeners:function(){this.on({start:this._startChildApps,"before:stop":this._stopChildApps,"before:destroy":this._destroyChildApps})},_startChildApps:function(){t.each(this._childApps,function(e){t.result(e,"startWithParent")&&e.start()})},_stopChildApps:function(){t.each(this._childApps,function(e){t.result(e,"stopWithParent")&&e.stop()})},startChildApp:function(t,e){return this.getChildApp(t).start(e),this},stopChildApp:function(t){return this.getChildApp(t).stop(),this},_destroyChildApps:function(){t.each(this._childApps,function(e){t.result(e,"preventDestroy")||e.destroy()})},_buildAppFromObject:function(e){var i=e.AppClass,n=t.omit(e,"AppClass");return this.buildApp(i,n)},_buildApp:function(e,i){return t.isFunction(e)?this.buildApp(e,i):t.isObject(e)?this._buildAppFromObject(e):void 0},buildApp:function(e,i){return i=t.extend({},this.childAppOptions,i),new e(i)},_ensureAppIsUnique:function(t){if(this._childApps[t])throw new e.Error({name:"DuplicateChildAppError",message:'A child App with name "'+t+'" has already been added.'})},addChildApps:function(e){t.each(e,t.bind(function(t,e){this.addChildApp(e,t)},this))},addChildApp:function(i,n,s){this._ensureAppIsUnique(i);var r=this._buildApp(n,s);if(!r)throw new e.Error({name:"AddChildAppError",message:"App build failed. Incorrect configuration."});return r._name=i,this._childApps[i]=r,r.on("destroy",t.partial(this._removeChildApp,i),this),this.isRunning()&&t.result(r,"startWithParent")&&r.start(),r},getName:function(){return this._name},getChildApps:function(){return t.clone(this._childApps)},getChildApp:function(t){return this._childApps[t]},_removeChildApp:function(t){delete this._childApps[t]._name,delete this._childApps[t]},removeChildApps:function(){var e=this.getChildApps();return t.each(this._childApps,t.bind(function(t,e){this.removeChildApp(e)},this)),e},removeChildApp:function(e,i){i=t.extend({},i);var n=this.getChildApp(e);if(n)return i.preventDestroy||t.result(n,"preventDestroy")?this._removeChildApp(e):n.destroy(),n}},h={_stopRunningEvents:function(){t.each(this._runningEvents,t.bind(function(t){this.off.apply(this,t)},this))},_stopRunningListeners:function(){t.each(this._runningListeningTo,t.bind(function(t){this.stopListening.apply(this,t)},this))},on:function(){return this._isRunning&&(this._runningEvents=this._runningEvents||[],this._runningEvents.push(arguments)),e.Object.prototype.on.apply(this,arguments)},listenTo:function(){return this._isRunning&&(this._runningListeningTo=this._runningListeningTo||[],this._runningListeningTo.push(arguments)),e.Object.prototype.listenTo.apply(this,arguments)},listenToOnce:function(){return this._isRunning&&(this._runningListeningTo=this._runningListeningTo||[],this._runningListeningTo.push(arguments)),e.Object.prototype.listenToOnce.apply(this,arguments)}},p=["startWithParent","stopWithParent","startAfterInitialized","preventDestroy","StateModel"],a=e.Application.extend({_isRunning:!1,preventDestroy:!1,startAfterInitialized:!1,startWithParent:!1,stopWithParent:!0,constructor:function(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.mergeOptions(i,p),this._initChildApps(i),e.Application.call(this,i),t.result(this,"startAfterInitialized")&&this.start(i)},_ensureAppIsIntact:function(){if(this._isDestroyed)throw new e.Error({name:"AppDestroyedError",message:"App has already been destroyed and cannot be used."})},isRunning:function(){return this._isRunning},start:function(e){if(this._ensureAppIsIntact(),this._isRunning)return this;var i=t.extend({},e);return this.setRegion(i.region),this.triggerMethod("before:start",e),i.state=this.getInitState(i.state),this.initState(i),this._isRunning=!0,this.triggerStart(i),this},setRegion:function(t){return t?(this._region=t,this):this},getInitState:function(t){return t},triggerStart:function(t){this.triggerMethod("start",t)},stop:function(t){return this._isRunning?(this.triggerMethod("before:stop",t),this._isRunning=!1,this._stopRunningListeners(),this._stopRunningEvents(),this.triggerMethod("stop",t),this):this},destroy:function(){this._isDestroyed||(this.stop(),e.Object.prototype.destroy.apply(this,arguments))}});t.extend(a.prototype,s,o,h);var d=["ViewClass","viewEventPrefix","viewOptions","region"],u=e.Object.extend({ViewClass:e.View,viewEventPrefix:"view",viewOptions:{},constructor:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.mergeOptions(t,d),this.initState(t),e.Object.call(this,t)},_shouldDestroy:!0,showIn:function(t,e){return this.region=t,this.show(e),this},show:function(t){var i=this.getRegion();if(this._isShown)throw new e.Error({name:"ComponentShowError",message:"Component has already been shown in a region."});if(!i)throw new e.Error({name:"ComponentRegionError",message:"Component has no defined region."});return this.triggerMethod("before:show"),this.renderView(t),this._isShown=!0,this.triggerMethod("show"),this.listenTo(i,"empty",this._destroy),this},getRegion:function(){return this.region},_getViewClass:function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},s=this.ViewClass;if(s.prototype instanceof i.View||s===i.View)return s;if(t.isFunction(s))return s.call(this,n);throw new e.Error({name:"InvalidViewClassError",message:'"ViewClass" must be a view class or a function that returns a view class'})},renderView:function(t){var e=this._getViewClass(t),i=this.mixinOptions(t),n=this.buildView(e,i);return this.currentView=n,this._proxyViewEvents(n),this.triggerMethod("before:render:view",n),this._shouldDestroy=!1,this.getRegion().show(n),this._shouldDestroy=!0,this.triggerMethod("render:view",n),this},_proxyViewEvents:function(e){var i=this.viewEventPrefix;e.on("all",function(){var n=t.toArray(arguments),s=n[0];n[0]=i+":"+s,n.splice(1,0,e),this.triggerMethod.apply(this,n)},this)},mixinOptions:function(e){var i=t.result(this,"viewOptions");return t.extend({state:this.getState().attributes},i,e)},buildView:function(t,e){return new t(e)},_destroy:function(){this._shouldDestroy&&e.Object.prototype.destroy.apply(this,arguments)},_emptyRegion:function(t){var e=this.getRegion();e&&(this.stopListening(e,"empty"),e.empty(t))},destroy:function(t){this._emptyRegion(t),this._shouldDestroy=!0,this._destroy(t)}});t.extend(u.prototype,s);var l=e.Toolkit,c=e.Toolkit={};return c.noConflict=function(){return e.Toolkit=l,this},c.MixinState=function(e){var i=s;e.prototype.StateModel&&(i=t.omit(s,"StateModel")),t.extend(e.prototype,i)},c.VERSION="3.0.0",c.StateMixin=s,c.App=a,c.Component=u,c}); | ||
//# sourceMappingURL=marionette.toolkit.min.js.map |
@@ -10,2 +10,3 @@ # Marionette.Toolkit.App | ||
## Documentation Index | ||
* [Using Toolkit App](#using-toolkit-app) | ||
* [Lifecycle Settings](#lifecycle-settings) | ||
@@ -24,5 +25,13 @@ * [App's `startAfterInitialized`](#apps-startafterinitialized) | ||
* ["before:stop" / "stop" events](#beforestop--stop-events) | ||
* [Application Region](#application-region) | ||
* [App `setRegion`](#app-setregion) | ||
* [Application State](#application-state) | ||
* [App `getInitState`](#app-getInitState) | ||
## Using Toolkit App | ||
Beyond the Toolkit App's lifecycle, its biggest feature is [childApps](./mixings/child-apps.md#apps-childapps). | ||
This allows children to be tied to the lifecycle of their parent's lifecycle. | ||
As Toolkit App extends [`Marionette.Application`](https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.md) it can be used as a drop-in replacement. | ||
## Lifecycle Settings | ||
@@ -318,2 +327,11 @@ | ||
## Application Region | ||
A `Marionette.Region` instance can be passed to [App `start`](#app-start) as an option, | ||
[setting the App region](#app-setregion), making it available to both [`before:start` and `start`](#beforestart--start-events) events. | ||
### App `setRegion` | ||
Calling `setRegion` will replace the `App`'s region making it available to the App's [Region API](https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.md#getregion). | ||
## Application State | ||
@@ -320,0 +338,0 @@ |
{ | ||
"name": "marionette.toolkit", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A collection of opinionated Backbone.Marionette extensions for large scale application architecture.", | ||
@@ -41,8 +41,8 @@ "main": "./dist/marionette.toolkit.js", | ||
"babel-register": "6.7.2", | ||
"backbone": "1.3.3", | ||
"backbone.marionette": "3.0.0", | ||
"backbone": "^1.3.3", | ||
"backbone.marionette": "^3.0.0", | ||
"chai": "2.0.0", | ||
"del": "1.1.1", | ||
"eslint": "2.8.0", | ||
"gulp": "3.8.10", | ||
"gulp": "3.9.1", | ||
"gulp-eslint": "2.0.0", | ||
@@ -71,3 +71,3 @@ "gulp-file": "0.2.0", | ||
"sinon-chai": "2.7.0", | ||
"underscore": "1.8.3" | ||
"underscore": "^1.8.3" | ||
}, | ||
@@ -79,6 +79,6 @@ "babelBoilerplateOptions": { | ||
"peerDependencies": { | ||
"backbone.marionette": "3.0.0", | ||
"backbone": "1.3.3", | ||
"underscore": "1.8.3" | ||
"backbone.marionette": "^3.0.0", | ||
"backbone": "^1.3.3", | ||
"underscore": "^1.8.3" | ||
} | ||
} |
Marionette.Toolkit | ||
================== | ||
[](https://travis-ci.org/RoundingWellOS/marionette.toolkit) [](https://codeclimate.com/github/RoundingWellOS/marionette.toolkit) [](https://codeclimate.com/github/RoundingWellOS/marionette.toolkit) [](https://david-dm.org/RoundingWellOS/marionette.toolkit) | ||
[](https://travis-ci.org/RoundingWellOS/marionette.toolkit) [](https://codeclimate.com/github/RoundingWellOS/marionette.toolkit) | ||
@@ -19,3 +19,3 @@ | ||
[Asnyc App start](./docs/async-app-start.md) - How to easily start apps asynchronously. | ||
[Async App start](./docs/async-app-start.md) - How to easily start apps asynchronously. | ||
@@ -64,7 +64,5 @@ [StateMixin](./docs/mixins/state.md) - JavaScript Object with a `Backbone.Model` for keeping state. Used by both the App and Component but can be applied to any Marionette Class. | ||
Marionette.Toolkit currently requires [Marionette](http://marionettejs.com) 3.0.0+ as it utilizes [`Marionette.Error`](https://github.com/marionettejs/backbone.marionette/blob/v2.2.0/src/marionette.error.js) and extends | ||
Marionette.Toolkit currently requires and is tested with [Marionette](http://marionettejs.com) 3.0.0+ as it extends | ||
[`Marionette.Application`](https://github.com/marionettejs/backbone.marionette/blob/next/docs/marionette.application.md) | ||
This library is tested with Marionette v3.0+ | ||
Marionette.Toolkit supports IE9+ and modern browsers. | ||
@@ -71,0 +69,0 @@ |
@@ -77,4 +77,2 @@ import _ from 'underscore'; | ||
constructor(options = {}) { | ||
_.bindAll(this, 'start', 'stop'); | ||
this.mergeOptions(options, ClassOptions); | ||
@@ -137,5 +135,8 @@ | ||
const opts = _.extend({}, options); | ||
this.setRegion(opts.region); | ||
this.triggerMethod('before:start', options); | ||
const opts = _.extend({}, options); | ||
opts.state = this.getInitState(opts.state); | ||
@@ -153,2 +154,22 @@ | ||
/** | ||
* Set the Application's Region after instantiation | ||
* | ||
* @public | ||
* @method setRegion | ||
* @memberOf App | ||
* @param {Region} [region] - Region to use with the app | ||
* @returns {App} | ||
*/ | ||
setRegion(region) { | ||
if(!region) { | ||
return this; | ||
} | ||
this._region = region; | ||
return this; | ||
}, | ||
/** | ||
* Returns state. | ||
@@ -204,7 +225,7 @@ * Override to extend state | ||
this.triggerMethod('stop', options); | ||
this._stopRunningListeners(); | ||
this._stopRunningEvents(); | ||
this.triggerMethod('stop', options); | ||
return this; | ||
@@ -211,0 +232,0 @@ }, |
@@ -0,1 +1,2 @@ | ||
import _ from 'underscore'; | ||
import Marionette from 'backbone.marionette'; | ||
@@ -2,0 +3,0 @@ |
@@ -101,3 +101,3 @@ import _ from 'underscore'; | ||
* @param {String} appName - Name of childApp to start | ||
* @param {object} options - Start options for app | ||
* @param {Object} options - Start options for app | ||
* @public | ||
@@ -215,5 +215,5 @@ * @method startChildApp | ||
addChildApps(childApps) { | ||
_.each(childApps, function(childApp, appName) { | ||
_.each(childApps, _.bind(function(childApp, appName) { | ||
this.addChildApp(appName, childApp); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -319,5 +319,5 @@ | ||
_.each(this._childApps, function(childApp, appName) { | ||
_.each(this._childApps, _.bind(function(childApp, appName) { | ||
this.removeChildApp(appName); | ||
}, this); | ||
}, this)); | ||
@@ -324,0 +324,0 @@ return childApps; |
@@ -19,5 +19,5 @@ import _ from 'underscore'; | ||
_stopRunningEvents() { | ||
_.each(this._runningEvents, function(args) { | ||
_.each(this._runningEvents, _.bind(function(args) { | ||
this.off.apply(this, args); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -32,5 +32,5 @@ | ||
_stopRunningListeners() { | ||
_.each(this._runningListeningTo, function(args) { | ||
_.each(this._runningListeningTo, _.bind(function(args) { | ||
this.stopListening.apply(this, args); | ||
}, this); | ||
}, this)); | ||
}, | ||
@@ -37,0 +37,0 @@ |
@@ -41,18 +41,2 @@ describe('ChildAppMixin', function() { | ||
describe('addChildApps', function() { | ||
beforeEach(function() { | ||
this.sinon.spy(this.myApp, 'addChildApps'); | ||
this.myApp.addChildApps(); | ||
}); | ||
it('should be called', function() { | ||
expect(this.myApp.addChildApps.called).to.be.true; | ||
}); | ||
it('should have no arguments', function() { | ||
expect(this.myApp.addChildApps.calledWith(this.childApps)).to.be.false; | ||
}); | ||
}); | ||
describe('addChildApp function', function() { | ||
@@ -93,5 +77,7 @@ it('should raise an error message', function() { | ||
}; | ||
this.sinon.spy(this.myApp, '_initChildApps'); | ||
this.myApp = new Marionette.Toolkit.App({ childApps: childApps }); | ||
const MyApp = Marionette.Toolkit.App.extend(); | ||
this.sinon.spy(MyApp.prototype, '_initChildApps'); | ||
this.myApp = new MyApp({ childApps: childApps }); | ||
expect(this.myApp._initChildApps.called); | ||
@@ -98,0 +84,0 @@ expect(_.keys(this.myApp._childApps)).to.have.length(3); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
267456
43
3486
87