marionette.toolkit
Advanced tools
Comparing version 3.0.1 to 3.1.0
{ | ||
"name": "marionette.toolkit", | ||
"version": "3.0.1", | ||
"version": "3.1.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,8 @@ | ||
#### v3.1.0 | ||
* `App` | ||
* Fix `stateEvents` when passed at App instantiation | ||
* Fix leaky lingering `stateEvents` after App stop | ||
* Add App `showChildView` / `getChildView` | ||
#### v3.0.1 | ||
@@ -2,0 +9,0 @@ |
/** | ||
* marionette.toolkit - A collection of opinionated Backbone.Marionette extensions for large scale application architecture. | ||
* @version v3.0.1 | ||
* @version v3.1.0 | ||
* @link https://github.com/RoundingWellOS/marionette.toolkit | ||
@@ -536,2 +536,48 @@ * @license MIT | ||
return childApp; | ||
}, | ||
/** | ||
* Shows a view in the region of the app's view | ||
* | ||
* @public | ||
* @method showChildView | ||
* @param {String} regionName - Name of region to show in | ||
* @param {View} view - Child view instance | ||
* @param {...args} Additional args that get passed along | ||
* @returns {View} - Child view instance | ||
*/ | ||
showChildView: function showChildView(regionName, view) { | ||
var appView = this.getView(); | ||
if (!appView) { | ||
return false; | ||
} | ||
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
args[_key - 2] = arguments[_key]; | ||
} | ||
appView.showChildView.apply(appView, [regionName, view].concat(args)); | ||
return view; | ||
}, | ||
/** | ||
* Returns view from the App view by region name. | ||
* | ||
* @public | ||
* @method getChildView | ||
* @param {String} regionName - Name of region to get view from | ||
* @returns {View} | ||
*/ | ||
getChildView: function getChildView(regionName) { | ||
var appView = this.getView(); | ||
if (!appView) { | ||
return false; | ||
} | ||
return appView.getChildView(regionName); | ||
} | ||
@@ -627,3 +673,3 @@ }; | ||
var ClassOptions$1 = ['startWithParent', 'stopWithParent', 'startAfterInitialized', 'preventDestroy', 'StateModel']; | ||
var ClassOptions$1 = ['startWithParent', 'stopWithParent', 'startAfterInitialized', 'preventDestroy', 'StateModel', 'stateEvents']; | ||
@@ -762,6 +808,6 @@ /** | ||
this._isRunning = true; | ||
this.initState(opts); | ||
this._isRunning = true; | ||
this.triggerStart(opts); | ||
@@ -1226,3 +1272,3 @@ | ||
Toolkit.VERSION = '3.0.1'; | ||
Toolkit.VERSION = '3.1.0'; | ||
@@ -1229,0 +1275,0 @@ Toolkit.StateMixin = StateMixin; |
/** | ||
* marionette.toolkit - A collection of opinionated Backbone.Marionette extensions for large scale application architecture. | ||
* @version v3.0.1 | ||
* @version v3.1.0 | ||
* @link https://github.com/RoundingWellOS/marionette.toolkit | ||
* @license MIT | ||
*/ | ||
!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.triggerMethod("stop",t),this._stopRunningListeners(),this._stopRunningEvents(),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.1",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},showChildView:function(t,e){var i=this.getView();if(!i)return!1;for(var n=arguments.length,s=Array(n>2?n-2:0),r=2;r<n;r++)s[r-2]=arguments[r];return i.showChildView.apply(i,[t,e].concat(s)),e},getChildView:function(t){var e=this.getView();return!!e&&e.getChildView(t)}},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","stateEvents"],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._isRunning=!0,this.initState(i),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.triggerMethod("stop",t),this._stopRunningListeners(),this._stopRunningEvents(),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.1.0",c.StateMixin=s,c.App=a,c.Component=u,c}); | ||
//# sourceMappingURL=marionette.toolkit.min.js.map |
@@ -27,3 +27,8 @@ # Marionette.Toolkit.App | ||
* [Application State](#application-state) | ||
* [App `getInitState`](#app-getInitState) | ||
* [App `getInitState`](#app-getinitstate) | ||
* [App `StateModel`](#app-statemodel) | ||
* [App `stateEvents`](#app-stateevents) | ||
* [Application View](#application-view) | ||
* [App `showChildView`](#app-showchildview) | ||
* [App `getChildView`](#app-getchildview) | ||
@@ -350,1 +355,56 @@ ## Using Toolkit App | ||
``` | ||
### App `StateModel` | ||
A [`StateModel`](./mixins/state.md#statemixins-statemodel) class can be passed to App instantiation as an option or defined on the App. | ||
```js | ||
var myApp = new MyApp({ | ||
StateModel: MyStateModel | ||
}); | ||
``` | ||
### App `stateEvents` | ||
A [`stateEvents`](./mixins/state.md#statemixins-stateevents) hash can be passed to App instantiation as an option or defined on the App. | ||
```js | ||
var MyApp = Marionette.Toolkit.App.extend({ | ||
stateEvents: { | ||
'change': 'onChangeState' | ||
} | ||
onChangeState: function() { | ||
// Handle state change event | ||
} | ||
}); | ||
``` | ||
## Application View | ||
A [`Marionette.Application`](https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.md) can have an associated view shown in its region with `showView`. Toolkit takes this a step further and proxies that view's `showChildView` and `getChildView`. This is simply sugar for common patterns. | ||
### App `showChildView` | ||
This method will help when a region from the App's view is needed. | ||
It has the same API as a `Marionette.View`'s `showChildView` and returns the show view. | ||
```js | ||
myApp.showChildView('fooRegion', myChildView, 'fooArg'); | ||
//is equivalent to | ||
myApp.getView().getRegion('fooRegion').show(myChildView, 'fooArg'); | ||
``` | ||
### App `getChildView` | ||
Like `showChildView`, `getChildView` is a helper for getting a view shown in a region belonging to the App's view. | ||
It has the same API as a `Marionette.View`'s `getChildView`. | ||
```js | ||
myApp.getChildView('fooRegion'); | ||
//is equivalent to | ||
myApp.getView().getRegion('fooRegion').currentView; | ||
``` |
{ | ||
"name": "marionette.toolkit", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A collection of opinionated Backbone.Marionette extensions for large scale application architecture.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/marionette.toolkit.js", |
@@ -12,3 +12,4 @@ import _ from 'underscore'; | ||
'preventDestroy', | ||
'StateModel' | ||
'StateModel', | ||
'stateEvents' | ||
]; | ||
@@ -143,6 +144,6 @@ | ||
this._isRunning = true; | ||
this.initState(opts); | ||
this._isRunning = true; | ||
this.triggerStart(opts); | ||
@@ -149,0 +150,0 @@ |
@@ -351,3 +351,43 @@ import _ from 'underscore'; | ||
return childApp; | ||
}, | ||
/** | ||
* Shows a view in the region of the app's view | ||
* | ||
* @public | ||
* @method showChildView | ||
* @param {String} regionName - Name of region to show in | ||
* @param {View} view - Child view instance | ||
* @param {...args} Additional args that get passed along | ||
* @returns {View} - Child view instance | ||
*/ | ||
showChildView(regionName, view, ...args) { | ||
const appView = this.getView(); | ||
if(!appView) { | ||
return false; | ||
} | ||
appView.showChildView(regionName, view, ...args); | ||
return view; | ||
}, | ||
/** | ||
* Returns view from the App view by region name. | ||
* | ||
* @public | ||
* @method getChildView | ||
* @param {String} regionName - Name of region to get view from | ||
* @returns {View} | ||
*/ | ||
getChildView(regionName) { | ||
const appView = this.getView(); | ||
if(!appView) { | ||
return false; | ||
} | ||
return appView.getChildView(regionName); | ||
} | ||
}; |
@@ -10,3 +10,8 @@ import App from '../../src/app'; | ||
this.destroyStub = this.sinon.stub(); | ||
this.myApp = new App(); | ||
this.onChangeStub = this.sinon.stub(); | ||
this.myApp = new App({ | ||
stateEvents: { | ||
'change': this.onChangeStub | ||
} | ||
}); | ||
this.myApp.on('before:start', this.beforeStartStub); | ||
@@ -76,2 +81,11 @@ this.myApp.on('start', this.startStub); | ||
describe('when triggering a stateEvent after app stop', function() { | ||
it('should not call the stateEvent', function() { | ||
this.myApp.stop(); | ||
this.myApp.setState('foo', 'bar'); | ||
expect(this.onChangeStub).to.have.not.been.called; | ||
}); | ||
}); | ||
describe('when stop listener is a running listener', function() { | ||
@@ -78,0 +92,0 @@ beforeEach(function() { |
@@ -21,2 +21,21 @@ import $ from 'jquery'; | ||
describe('when passing stateEvents on Toolkit.App definition', function() { | ||
before(function() { | ||
this.onChangeStub = this.sinon.stub(); | ||
this.myApp = new App({ | ||
stateEvents: { | ||
'change': this.onChangeStub | ||
} | ||
}); | ||
this.myApp.start(); | ||
}); | ||
it('should successfully user stateEvents', function() { | ||
this.myApp.setState('foo', 'bar'); | ||
expect(this.onChangeStub).to.have.been.calledOnce; | ||
}); | ||
}); | ||
describe('#setRegion', function() { | ||
@@ -68,2 +87,97 @@ beforeEach(function() { | ||
}); | ||
describe('#showChildView', function() { | ||
beforeEach(function() { | ||
const MyApp = App.extend(); | ||
this.sinon.spy(MyApp.prototype, 'showChildView'); | ||
this.myApp = new MyApp({ | ||
region: new Marionette.Region({ el: $('<div>')[0] }) | ||
}); | ||
}); | ||
describe('when the app region has a view', function() { | ||
beforeEach(function() { | ||
const myView = new Marionette.View({ | ||
regions: { | ||
someRegion: 'div' | ||
}, | ||
template: _.template('<div></div>') | ||
}); | ||
this.sinon.spy(myView, 'showChildView'); | ||
this.myApp.showView(myView); | ||
this.view = new Marionette.View({ template: false }); | ||
this.myApp.showChildView('someRegion', this.view, 'foo'); | ||
}); | ||
it('should return the child view', function() { | ||
expect(this.myApp.showChildView.returned(this.view)).to.be.true; | ||
}); | ||
it('should call showChildView on the app\'s view', function() { | ||
expect(this.myApp.getView().showChildView).to.have.been.calledWith('someRegion', this.view, 'foo'); | ||
}); | ||
}); | ||
describe('when the app region does not have a view', function() { | ||
it('should return false', function() { | ||
this.myApp.showChildView('someRegion', new Marionette.View()); | ||
expect(this.myApp.showChildView.returned(false)).to.be.true; | ||
}); | ||
}); | ||
}); | ||
describe('#getChildView', function() { | ||
beforeEach(function() { | ||
const MyApp = App.extend(); | ||
this.sinon.spy(MyApp.prototype, 'getChildView'); | ||
this.myApp = new MyApp({ | ||
region: new Marionette.Region({ el: $('<div>')[0] }) | ||
}); | ||
}); | ||
describe('when the app region has a view', function() { | ||
beforeEach(function() { | ||
const myView = new Marionette.View({ | ||
regions: { | ||
someRegion: 'div' | ||
}, | ||
template: _.template('<div></div>') | ||
}); | ||
this.sinon.spy(myView, 'getChildView'); | ||
this.myApp.showView(myView); | ||
this.view = new Marionette.View({ template: false }); | ||
this.myApp.showChildView('someRegion', this.view); | ||
this.myApp.getChildView('someRegion'); | ||
}); | ||
it('should return the child view', function() { | ||
expect(this.myApp.getChildView.returned(this.view)).to.be.true; | ||
}); | ||
it('should call showChildView on the app\'s view', function() { | ||
expect(this.myApp.getView().getChildView).to.have.been.calledWith('someRegion'); | ||
}); | ||
}); | ||
describe('when the app region does not have a view', function() { | ||
it('should return false', function() { | ||
this.myApp.getChildView('someRegion'); | ||
expect(this.myApp.getChildView.returned(false)).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
279220
3669