Skip to content

Commit d4eca31

Browse files
committed
Update demos and docs for new injectorModules feature and dependency injection on resolve functions.
1 parent 3a47e13 commit d4eca31

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ deferredBootstrapper.bootstrap({
2020
element: document.body,
2121
module: 'MyApp',
2222
resolve: {
23-
APP_CONFIG: function ($http) {
23+
APP_CONFIG: ['$http', function ($http) {
2424
return $http.get('/api/demo-config');
25-
}
25+
}]
2626
}
2727
});
2828
```
@@ -44,7 +44,7 @@ To make it possible to conditionally show a loading indicator or an error messag
4444
Have a look at the demo pages to see this in action.
4545

4646
## Advanced usage
47-
You can have multiple constants resolved for your app and you can do in the resolve function whatever is necessary before the app is started. The only constraint is, that the function has to return a promise. It is important to note, that the arguments passed to your resolve functions are NOT dependency injected. You get access to the following services in the resolve function: $http, $q, injector
47+
You can have multiple constants resolved for your app and you can do in the resolve function whatever is necessary before the app is started. The only constraint is, that the function has to return a promise.
4848

4949
To handle exceptions when the promises are resolved, you can add an onError function to the configuration object.
5050

@@ -54,23 +54,57 @@ deferredBootstrapper.bootstrap({
5454
element: document.body,
5555
module: 'MyApp',
5656
resolve: {
57-
APP_CONFIG: function ($http) {
57+
APP_CONFIG: ['$http', function ($http) {
5858
return $http.get('/api/demo-config');
59-
},
60-
OTHER_CONSTANT: function ($http, $q, injector) {
61-
var deferred = $q.defer(), $timeout = injector.get('$timeout');
59+
}],
60+
OTHER_CONSTANT: ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
61+
var deferred = $q.defer();
6262
$timeout(function () {
6363
deferred.resolve('MyConstant');
6464
}, 2000);
6565
return deferred.promise;
66-
}
66+
}]
6767
},
6868
onError: function (error) {
6969
alert('Could not bootstrap, error: ' + error);
7070
}
7171
});
7272
```
7373

74+
## Custom injector modules
75+
By default, the injector that calls your resolve functions only provides the services from the AngularJS core module (ng). If you have a use case where you want to use one of your existing services to get configuration at bootstrap time, you can specify which modules should be made available and inject services from those modules in the resolve function. An example is below:
76+
77+
```js
78+
deferredBootstrapper.bootstrap({
79+
element: document.body,
80+
module: 'myApp',
81+
injectorModules: 'myApp.settings',
82+
resolve: {
83+
SETTINGS: ['SettingsService', function (SettingsService) {
84+
return SettingsService.get('/settings');
85+
}]
86+
}
87+
});
88+
```
89+
90+
The ```injectorModules``` option can also take an array of modules. If you have multiple services spread across different modules you can also inject them:
91+
92+
```js
93+
deferredBootstrapper.bootstrap({
94+
element: document.body,
95+
module: 'myApp',
96+
injectorModules: ['myApp.settings', 'myApp.foo']
97+
resolve: {
98+
SETTINGS: ['SettingsService', function (SettingsService) {
99+
return SettingsService.get('/settings');
100+
}],
101+
FOO: ['FooService', function (FooService) {
102+
return FooService.get('/foo');
103+
}]
104+
}
105+
});
106+
```
107+
74108
## Testing
75109
Since the constants that deferredBootstrapper adds to your applications module are not available in your unit tests, it makes sense to provide them in a global beforeEach():
76110
```js

demo/error-handler.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
element: window.document.body,
3838
module: 'demoApp',
3939
resolve: {
40-
ERROR: function ($http, $q, injector) {
41-
var deferred = $q.defer(), $timeout = injector.get('$timeout');
40+
ERROR: ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
41+
var deferred = $q.defer();
4242
$timeout(function () {
4343
deferred.reject('EXCEPTION');
4444
}, 2000);
4545
return deferred.promise;
46-
}
46+
}]
4747
},
4848
onError: function(error) {
4949
alert('could not start app: ' + error);

demo/error.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
element: window.document.body,
3838
module: 'demoApp',
3939
resolve: {
40-
ERROR: function ($http, $q, injector) {
41-
var deferred = $q.defer(), $timeout = injector.get('$timeout');
40+
ERROR: ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
41+
var deferred = $q.defer();
4242
$timeout(function () {
4343
deferred.reject('EXCEPTION');
4444
}, 2000);
4545
return deferred.promise;
46-
}
46+
}]
4747
}
4848
});
4949

demo/multi.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@
4141
element: window.document.body,
4242
module: 'demoApp',
4343
resolve: {
44-
STARTUP_CONFIG: function ($http) {
44+
STARTUP_CONFIG: ['$http', function ($http) {
4545
return $http.get('/api/demo-config');
46-
},
47-
OTHER_CONFIG: function ($http) {
46+
}],
47+
OTHER_CONFIG: ['$http', function ($http) {
4848
return $http.get('/api/demo-config-2');
49-
},
50-
USING_Q: function ($http, $q, injector) {
51-
var deferred = $q.defer(), $timeout = injector.get('$timeout');
49+
}],
50+
USING_Q: ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
51+
var deferred = $q.defer();
5252
$timeout(function () {
5353
deferred.resolve('MyConstant');
5454
}, 2000);
5555
return deferred.promise;
56-
}
56+
}]
5757
}
5858
});
5959

demo/simple.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
element: window.document.body,
4242
module: 'demoApp',
4343
resolve: {
44-
STARTUP_CONFIG: function ($http) {
44+
STARTUP_CONFIG: ['$http', function ($http) {
4545
return $http.get('/api/demo-config');
46-
}
46+
}]
4747
}
4848
});
4949

0 commit comments

Comments
 (0)