Skip to content

Commit 51c1bb5

Browse files
committed
Merge pull request #22 from B8li/master
Fix bug when injecting $location.
2 parents 9c5964a + f6ee37a commit 51c1bb5

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

.jshintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"predef": ["describe", "it", "beforeEach", "waitsFor", "expect", "angular"],
2+
"predef": ["describe", "it", "beforeEach", "waitsFor", "expect", "angular", "spyOn", "jasmine"],
33
"esnext": false,
44
"bitwise": true,
55
"curly": true,

src/deferred-bootstrap.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ var isObject = angular.isObject,
55
isArray = angular.isArray,
66
isString = angular.isString,
77
forEach = angular.forEach,
8-
ngInjector = angular.injector(['ng']),
9-
$q = ngInjector.get('$q'),
10-
bodyElement,
118
loadingClass = 'deferred-bootstrap-loading',
12-
errorClass = 'deferred-bootstrap-error';
9+
errorClass = 'deferred-bootstrap-error',
10+
bodyElement,
11+
$q;
1312

1413
function addLoadingClass () {
1514
bodyElement.addClass(loadingClass);
@@ -63,16 +62,22 @@ function checkConfig (config) {
6362
throw new Error('\'config.onError\' must be a function.');
6463
}
6564
}
65+
function provideRootElement (modules, element) {
66+
element = angular.element(element);
67+
modules.unshift(['$provide', function($provide) {
68+
$provide.value('$rootElement', element);
69+
}]);
70+
}
6671

67-
function createInjector (injectorModules) {
72+
function createInjector (injectorModules, element) {
73+
var modules = ['ng'];
6874
if (isString(injectorModules)) {
69-
return angular.injector(['ng', injectorModules]);
70-
} else if (isArray(injectorModules) && injectorModules.length === 1 && injectorModules[0] === 'ng') {
71-
return ngInjector;
72-
} else {
73-
injectorModules.unshift('ng');
74-
return angular.injector(injectorModules);
75+
modules.push(injectorModules);
76+
} else if (isArray(injectorModules)) {
77+
modules = modules.concat(injectorModules);
7578
}
79+
provideRootElement(modules, element);
80+
return angular.injector(modules, element);
7681
}
7782

7883
function doBootstrap (element, module) {
@@ -101,7 +106,8 @@ function bootstrap (configParam) {
101106

102107
addLoadingClass();
103108
checkConfig(config);
104-
injector = createInjector(injectorModules);
109+
injector = createInjector(injectorModules, element);
110+
$q = injector.get('$q');
105111

106112
function callResolveFn (resolveFunction, constantName, moduleName) {
107113
var result;

test/deferred-bootstrap.spec.js

+75-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function itAsync(title, func) {
1717
});
1818
}
1919

20-
/* global checkConfig, isPromise, loadingClass, errorClass */
20+
/* global checkConfig, createInjector, isPromise, loadingClass, errorClass */
2121
describe('deferredBootstrapper', function () {
2222

2323
it('should provide bootstrap function', function () {
@@ -35,6 +35,29 @@ describe('deferredBootstrapper', function () {
3535
bodyElement = window.document.body;
3636
});
3737

38+
itAsync('should inject $location', function (done) {
39+
bootstrap({
40+
element: bodyElement,
41+
module: APP_NAME,
42+
resolve: {
43+
LOCATION: function ($http, $q, $location) {
44+
var deferred = $q.defer();
45+
46+
deferred.resolve($location);
47+
48+
return deferred.promise;
49+
}
50+
}
51+
});
52+
53+
angular.module(APP_NAME, [])
54+
.config(function (LOCATION) {
55+
expect(LOCATION).toBeDefined();
56+
57+
done();
58+
});
59+
});
60+
3861
itAsync('should resolve with the value returned from the defined constant', function (done) {
3962
bootstrap({
4063
element: bodyElement,
@@ -422,6 +445,57 @@ describe('deferredBootstrapper', function () {
422445

423446
});
424447

448+
describe('createInjector()', function () {
449+
var element;
450+
451+
beforeEach(function () {
452+
angular.module('module1', []);
453+
angular.module('module2', []);
454+
element = angular.element('<div></div>');
455+
});
456+
457+
it('should create injector with given module as string', function () {
458+
// given
459+
var modules = 'module1';
460+
spyOn(angular, 'injector').andCallThrough();
461+
462+
// when
463+
createInjector(modules, element);
464+
var injectorModules = angular.injector.mostRecentCall.args[0];
465+
466+
// then
467+
expect(injectorModules.indexOf('ng')).not.toBe(-1);
468+
expect(injectorModules.indexOf('module1')).not.toBe(-1);
469+
});
470+
471+
it('should create injector with given modules as array', function () {
472+
// given
473+
var modules = ['module1', 'module2'];
474+
spyOn(angular, 'injector').andCallThrough();
475+
476+
// when
477+
createInjector(modules, element);
478+
var injectorModules = angular.injector.mostRecentCall.args[0];
479+
480+
// then
481+
expect(injectorModules.indexOf('ng')).not.toBe(-1);
482+
expect(injectorModules.indexOf('module1')).not.toBe(-1);
483+
expect(injectorModules.indexOf('module2')).not.toBe(-1);
484+
});
485+
486+
it('should create injector with given element', function () {
487+
// given
488+
var modules = 'module1';
489+
spyOn(angular, 'injector').andCallThrough();
490+
491+
// when
492+
createInjector(modules, element);
493+
494+
// then
495+
expect(angular.injector).toHaveBeenCalledWith(jasmine.any(Object), element);
496+
});
497+
});
498+
425499
describe('isPromise()', function () {
426500

427501
it('should check if object is a promise', function () {

0 commit comments

Comments
 (0)