-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-store.js
55 lines (43 loc) · 1.7 KB
/
module-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* global define */
define([
'underscore',
'marionette'
], function(_, Marionette) {
'use strict';
/**
* ModuleStore is a drop-in replacement for
* Marionette.Application.module to allow development of standalone
* Modules which can be used with RequireJS and defined without
* the dependency on an Application instance.
*
* The module function is borrowed from Marionette.Application
* (https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.application.js)
* and amended to use the singleton rootModule variable for storing
* definitions if modules are loaded without the Application.
* An Application instance can make itself the rootModule using
* @see setRootModule
* This allows us to unit test modules with a dummy App instance.
*/
var rootModule = rootModule || {};
rootModule.submodules = rootModule.submodules || {};
var ModuleStore = {
module: function(moduleNames, moduleDefinition) {
var ModuleClass = Marionette.Module.getClass(moduleDefinition);
var args = Array.prototype.slice.call(arguments);
args.unshift(rootModule);
return ModuleClass.create.apply(ModuleClass, args);
},
setRootModule: function(newRoot) {
//attach all of our modules to the newRoot and
//then overwrite rootModule with this variable.
if(!newRoot) {
throw 'Can\'t set a Root Module to ' + newRoot;
}
rootModule = _.extend(newRoot, rootModule);
},
getRootModule: function() {
return rootModule;
}
};
return ModuleStore;
});