Skip to content

Commit e8b74af

Browse files
author
Marcos Lin
committed
Fixed the bug where modules created by alt_angular always return undefined when retrieved using angular.module. alt_angular will now return the module during processQueue but undefined after processQueue completes. Added associated unit test. This closes #67
1 parent ff032aa commit e8b74af

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

src/angularAMD.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
define(function () {
1111
var orig_angular,
1212
alt_angular,
13+
alternateModules = {},
14+
alternateModulesTracker = {},
1315
alternate_queue = [],
1416
app_name,
1517
app_injector,
@@ -36,13 +38,11 @@ define(function () {
3638
* object into the current ng-app. As result, if there are subsequent call to retrieve the
3739
* module post processQueue, it would retrieve a module that is not integrated into the ng-app.
3840
*
39-
* Therefore, any subsequent angular.module call to retrieve the module created with alternate
40-
* angular will return undefined.
41-
*
41+
* Therefore, any subsequent to call to angular.module after processQueue should return undefined
42+
* to prevent obtaining a duplicated object. However, it is critical that angular.module return
43+
* appropriate object *during* processQueue.
4244
*/
4345
function setAlternateAngular() {
44-
var alternateModules = {};
45-
4646
// This method cannot be called more than once
4747
if (alt_angular) {
4848
throw Error("setAlternateAngular can only be called once.");
@@ -58,11 +58,10 @@ define(function () {
5858

5959
// Custom version of angular.module used as cache
6060
alt_angular.module = function (name, requires) {
61-
6261
if (typeof requires === "undefined") {
63-
// Return undefined if module was created using the alt_angular
64-
if (alternateModules.hasOwnProperty(name)) {
65-
return undefined;
62+
// Return module from alternateModules if it was created using the alt_angular
63+
if (alternateModulesTracker.hasOwnProperty(name)) {
64+
return alternateModules[name];
6665
} else {
6766
return orig_angular.module(name);
6867
}
@@ -71,7 +70,16 @@ define(function () {
7170
var orig_mod = orig_angular.module.apply(null, arguments),
7271
item = { name: name, module: orig_mod};
7372
alternate_queue.push(item);
73+
74+
/*
75+
Use `alternateModulesTracker` to track which module has been created by alt_angular
76+
but use `alternateModules` to cache the module created. This is to simplify the
77+
removal of cached modules after .processQueue.
78+
*/
79+
alternateModulesTracker[name] = true;
7480
alternateModules[name] = orig_mod;
81+
82+
// Return created module
7583
return orig_mod;
7684
}
7785
};
@@ -81,7 +89,7 @@ define(function () {
8189

8290

8391
// Constructor
84-
function angularAMD() {}
92+
function angularAMD() {};
8593

8694

8795
/**
@@ -193,8 +201,11 @@ define(function () {
193201
});
194202
}
195203

196-
// How to remove the module???
197-
orig_angular.module(item.name, [], orig_angular.noop);
204+
/*
205+
Clear the cached modules created by alt_angular so that subsequent call to
206+
angular.module will return undefined.
207+
*/
208+
alternateModules = {};
198209
}
199210

200211
};

test/unit/controller.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ define(['controller', 'angularAMD'], function (app, angularAMD) {
3737
expect(s.name).toBe(service_results.service_name);
3838
expect(s.val_name).toBe(service_results.value_name);
3939
});
40+
41+
it("scope.subModule check", function () {
42+
// console.log("scope.subModule check", service_results.sub_module);
43+
expect(scope.utest_sub_module()).toBe(service_results.sub_module);
44+
});
4045

4146
});
4247
});

test/unit/lib/controller.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
define(['app','ngload!services'], function (app) {
55
'use strict';
66
var ctrl_name = "MainController";
7-
app.controller(ctrl_name, ['$scope', 'UtestFactory', 'UtestService', function ($scope, UtestFactory, UtestService) {
7+
app.controller(ctrl_name, function ($scope, UtestFactory, UtestService, UtestSubModule) {
88
$scope.ctrl_name = ctrl_name;
99
$scope.utest_factory = UtestFactory;
1010
$scope.utest_service = UtestService;
11-
}]);
11+
$scope.utest_sub_module = UtestSubModule;
12+
});
1213

1314
// Return expected unit test result
1415
app.__utest_ctrl_result = {

test/unit/lib/services.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
/*jslint node: true, nomen: true */
22
/*globals angular */
33

4-
(function () {
4+
!function () {
55
'use strict';
6-
76
// Services coded using regular angular approach
8-
var services = angular.module("utestServices", []),
7+
var sub_module = angular.module("subModuleServices", []),
8+
services = angular.module("utestServices", ['subModuleServices']),
99
utest_result = {};
10+
11+
// Define a simple subModule
12+
angular.module("subModuleServices").factory("SubConfigValue", function () {
13+
return function () {
14+
return "JwU9YSJMJS-HhRz4nhBuY";
15+
};
16+
});
1017

1118
// Make sure provider works
1219
services.provider("configUtest", function () {
@@ -30,7 +37,7 @@
3037
services.config(function (configUtestProvider) {
3138
configUtestProvider.configureValue(utest_result.config_name);
3239
});
33-
40+
3441
utest_result.run_name = "services.run sOdq6GNsaW";
3542
services.run(function ($rootScope) {
3643
$rootScope.run_name = utest_result.run_name;
@@ -72,6 +79,13 @@
7279
};
7380
});
7481

82+
utest_result.sub_module = "utestServices.sub_config_value JwU9YSJMJS-HhRz4nhBuY";
83+
services.factory('UtestSubModule', function (SubConfigValue) {
84+
return function () {
85+
return "utestServices.sub_config_value " + SubConfigValue();
86+
};
87+
});
88+
7589
// Return the result in a factory
7690
services.factory('UtestServiceResult', function () {
7791
return utest_result;
@@ -82,5 +96,5 @@
8296
return configUtest.getValue();
8397
});
8498

85-
}());
99+
}();
86100

test/unit/services.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,13 @@ define(['angularAMD', 'ngload!services'], function (angularAMD) {
6363
expect(ufilter("hello")).toBe("hello " + results.filter_name);
6464
}));
6565

66+
it("sub module check.", inject(function (UtestSubModule) {
67+
expect(UtestSubModule()).toBe(results.sub_module);
68+
}));
69+
70+
it("sub module check module.", function () {
71+
expect(angular.module("subModuleServices")).toBeUndefined();
72+
});
73+
6674
});
6775
});

0 commit comments

Comments
 (0)