Skip to content

Commit d3ebfed

Browse files
author
Marcos Lin
committed
Added angularAMD.config for post bootstrap config. This closes #71
1 parent 1c7922b commit d3ebfed

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

src/angularAMD.js

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66

77
define(function () {
88
var bootstrapped = false,
9-
orig_angular,
10-
alt_angular,
9+
10+
// Used in .bootstrap
11+
app_name,
1112
orig_app,
1213
alt_app,
13-
alternateModules = {},
14-
alternateModulesTracker = {},
15-
alternate_queue = [],
16-
app_name,
1714
run_injector,
1815
config_injector,
19-
app_cached_providers = {};
16+
app_cached_providers = {},
2017

18+
// Used in setAlternateAngular(), alt_angular is set to become angular.module
19+
orig_angular,
20+
alt_angular,
21+
22+
// Used in setAlternateAngular() and .processQueue
23+
alternate_modules = {},
24+
alternate_modules_tracker = {},
25+
alternate_queue = [],
26+
27+
// Used by angularAMD provider methods and .bootstrap
28+
deferred_providers = [];
2129

2230
// Private method to check if angularAMD has been initialized
2331
function checkBootstrapped() {
@@ -54,15 +62,15 @@ define(function () {
5462
// Make sure that bootstrap has been called
5563
checkBootstrapped();
5664

57-
// Createa a copy of orig_angular
65+
// Create a a copy of orig_angular
5866
orig_angular.extend(alt_angular, orig_angular);
5967

6068
// Custom version of angular.module used as cache
6169
alt_angular.module = function (name, requires) {
6270
if (typeof requires === "undefined") {
63-
// Return module from alternateModules if it was created using the alt_angular
64-
if (alternateModulesTracker.hasOwnProperty(name)) {
65-
return alternateModules[name];
71+
// Return module from alternate_modules if it was created using the alt_angular
72+
if (alternate_modules_tracker.hasOwnProperty(name)) {
73+
return alternate_modules[name];
6674
} else {
6775
return orig_angular.module(name);
6876
}
@@ -73,12 +81,12 @@ define(function () {
7381
alternate_queue.push(item);
7482

7583
/*
76-
Use `alternateModulesTracker` to track which module has been created by alt_angular
77-
but use `alternateModules` to cache the module created. This is to simplify the
84+
Use `alternate_modules_tracker` to track which module has been created by alt_angular
85+
but use `alternate_modules` to cache the module created. This is to simplify the
7886
removal of cached modules after .processQueue.
7987
*/
80-
alternateModulesTracker[name] = true;
81-
alternateModules[name] = orig_mod;
88+
alternate_modules_tracker[name] = true;
89+
alternate_modules[name] = orig_mod;
8290

8391
// Return created module
8492
return orig_mod;
@@ -134,14 +142,13 @@ define(function () {
134142
}];
135143
config.resolve = resolve;
136144
}
137-
138-
145+
139146
return config;
140147
};
141148

142149

143150
/**
144-
* Expose name of the app that has been bootstraped
151+
* Expose name of the app that has been bootstrapped
145152
*/
146153
angularAMD.prototype.appname = function () {
147154
checkBootstrapped();
@@ -206,7 +213,7 @@ define(function () {
206213
Clear the cached modules created by alt_angular so that subsequent call to
207214
angular.module will return undefined.
208215
*/
209-
alternateModules = {};
216+
alternate_modules = {};
210217
}
211218

212219
};
@@ -218,17 +225,26 @@ define(function () {
218225
angularAMD.prototype.getCachedProvider = function (provider_name) {
219226
checkBootstrapped();
220227
// Hack used for unit testing that orig_angular has been captured
221-
if (provider_name === "__orig_angular") {
222-
return orig_angular;
223-
} else if (provider_name === "__alt_angular") {
224-
return alt_angular;
225-
} else if (provider_name === "__orig_app") {
226-
return orig_app;
227-
} else if (provider_name === "__alt_app") {
228-
return alt_app;
229-
} else {
230-
return app_cached_providers[provider_name];
228+
var cachedProvider;
229+
230+
switch(provider_name) {
231+
case "__orig_angular":
232+
cachedProvider = orig_angular;
233+
break;
234+
case "__alt_angular":
235+
cachedProvider = alt_angular;
236+
break;
237+
case "__orig_app":
238+
cachedProvider = orig_app;
239+
break;
240+
case "__alt_app":
241+
cachedProvider = alt_app;
242+
break;
243+
default:
244+
cachedProvider = app_cached_providers[provider_name];
231245
}
246+
247+
return cachedProvider;
232248
};
233249

234250
/**
@@ -239,6 +255,16 @@ define(function () {
239255
checkBootstrapped();
240256
return run_injector.invoke.apply(null, arguments);
241257
};
258+
259+
260+
/**
261+
* Create config function that uses cached config_injector.
262+
* Designed to simulate app.config.
263+
*/
264+
angularAMD.prototype.config = function () {
265+
checkBootstrapped();
266+
return config_injector.invoke.apply(null, arguments);
267+
};
242268

243269
/**
244270
* Reset angularAMD for resuse
@@ -383,8 +409,6 @@ define(function () {
383409
setAlternateAngular();
384410
}
385411

386-
387-
388412
// Return app
389413
return alt_app;
390414
};

src/ngload.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
License: MIT
55
*/
66

7-
/*jslint node: true, vars: true, nomen: true */
8-
/*globals define */
9-
107
'use strict';
118
define({
129
load: function (name, req, onload, config) {

test/unit/services.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ define(['angularAMD', 'ngload!services'], function (angularAMD) {
6262
expect(ufilter).toBeDefined();
6363
expect(ufilter("hello")).toBe("hello " + results.filter_name);
6464
}));
65+
66+
it("angularAMD.config check.", function () {
67+
var configVal = "B9NmTuDAeU-JO0S1yjKXc";
68+
angularAMD.config(function (UtestStoreProvider) {
69+
UtestStoreProvider.configureValue(configVal);
70+
});
71+
inject(function (UtestStore) {
72+
expect(UtestStore.getValue()).toBe(configVal);
73+
});
74+
75+
});
6576

6677
it("sub module check.", inject(function (UtestSubModule) {
6778
expect(UtestSubModule.get()).toBe(results.sub_module);

0 commit comments

Comments
 (0)