-
Notifications
You must be signed in to change notification settings - Fork 68
/
wire.js
99 lines (86 loc) · 3.19 KB
/
wire.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** @license MIT License (c) copyright 2011-2013 original author or authors */
/**
* wire
* Javascript IOC Container
*
* wire is part of the cujoJS family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author Brian Cavalier
* @author John Hann
* @version 0.10.11
*/
(function(rootSpec, define){ 'use strict';
define(function(require) {
var createContext, rootContext, rootOptions;
wire.version = '0.10.11';
createContext = require('./lib/context');
rootOptions = { require: require };
/**
* Main Programmtic API. The top-level wire function that wires contexts
* as direct children of the (possibly implicit) root context. It ensures
* that the root context has been wired before wiring children.
*
* @public
*
* @param spec {Object|String|Array|Promise} can be any one of the following:
* 1. Object - wiring spec
* 2. String - module id of the wiring spec to load and then wire
* 3. Array - mixed array of Strings and Objects, each of which is either
* a wiring spec, or the module id of a wiring spec
* 4. Promise - a promise for any of the above
* @param options {Object} wiring options
* @param [options.require] {Function} the platform loader function. Wire will
* attempt to automatically detect what loader to use (AMD, CommonJS, etc.), but
* if you want to explicitly provide it, you can do so. In some cases this can
* be useful such as providing a local AMD require function so that module ids
* *within the wiring spec* can be relative.
* @return {Promise} a promise for the resulting wired context
*/
function wire(spec, options) {
// If the root context is not yet wired, wire it first
if (!rootContext) {
rootContext = createContext(rootSpec, null, rootOptions);
}
// Use the rootContext to wire all new contexts.
return rootContext.then(function (root) {
return root.wire(spec, options);
});
}
/**
* AMD Loader plugin API
* @param name {String} spec module id, or comma-separated list of module ids
* @param require {Function} loader-provided local require function
* @param done {Function} loader-provided callback to call when wiring
* is completed. May have and error property that a function to call to
* inform the AMD loader of an error.
* See here:
* https://groups.google.com/forum/?fromgroups#!topic/amd-implement/u0f161drdJA
*/
wire.load = function amdLoad(name, require, done /*, config */) {
// If it's a string, try to split on ',' since it could be a comma-separated
// list of spec module ids
wire(name.split(','), { require: require })
.then(done, done.error)
.otherwise(crash);
function crash(e) {
// Throw uncatchable exception for loaders that don't support
// AMD error handling. This will propagate up to the host environment
setTimeout(function() { throw e; }, 0);
}
};
/**
* AMD Builder plugin API
*/
// pluginBuilder: './builder/rjs'
wire['pluginBuilder'] = './builder/rjs';
wire['cramPlugin'] = './builder/cram';
return wire;
});
})(
this['wire'] || {},
typeof define == 'function' && define.amd
? define : function(factory) { module.exports = factory(require); }
);