4
4
< meta charset ="utf-8 " />
5
5
6
6
< title >
7
- Loading AngularJS Components After Application Bootstrap
7
+ Loading AngularJS Components With RequireJS After Application Bootstrap
8
8
</ title >
9
9
10
10
< style type ="text/css ">
24
24
< body ng-controller ="AppController ">
25
25
26
26
< h1 >
27
- Loading AngularJS Components After Application Bootstrap
27
+ Loading AngularJS Components With RequireJS After Application Bootstrap
28
28
</ h1 >
29
29
30
30
< p >
38
38
bootstrapped.
39
39
-->
40
40
< div ng-switch ="subview ">
41
- < div ng-switch-when ="before " ng-include =" 'before.htm' "> </ div >
42
- < div ng-switch-when ="after " ng-include =" 'after.htm' "> </ div >
43
- </ div >
44
-
41
+
42
+ < div ng-switch-when ="before ">
45
43
46
- <!-- BEGIN: Templates For ngInclude. -->
47
- < script type ="text/ng-template " id ="before.htm ">
44
+ < p >
45
+ Before app bootstrap.
46
+ </ p >
48
47
49
- < p >
50
- Before app bootstrap.
51
- </ p >
48
+ </ div >
49
+
50
+ < div ng-switch-when ="after " ng-include =" 'after.htm' ">
51
+ <!-- To be poprulated with the Lazy module content. -->
52
+ </ div >
52
53
53
- </ script >
54
- <!-- END: Templates For ngInclude. -->
54
+ </ div >
55
55
56
56
57
57
<!-- Load jQuery and AngularJS. -->
81
81
app . config (
82
82
function ( $controllerProvider , $provide , $compileProvider ) {
83
83
84
- // Since the "shorthand" methods for component
85
- // definitions are no longer valid, we can just
86
- // override them to use the providers for post-
87
- // bootstrap loading.
88
- console . log ( "Config method executed." ) ;
89
-
90
84
// Let's keep the older references.
91
85
app . _controller = app . controller ;
92
86
app . _service = app . service ;
@@ -146,53 +140,10 @@ <h1>
146
140
// -------------------------------------------------- //
147
141
148
142
149
- app . run (
150
- function ( $rootScope , $templateCache , $q ) {
151
-
152
- $rootScope . withLazyModule = function ( callback ) {
153
-
154
- require (
155
- [
156
- "../../vendor/require/text!lazy.htm" ,
157
- "lazy"
158
- ] ,
159
- function ( templates ) {
160
-
161
- $ ( templates ) . each (
162
- function ( ) {
163
-
164
- var template = $ ( this ) ;
165
-
166
- $templateCache . put ( template . attr ( "id" ) , template . html ( ) ) ;
167
-
168
- }
169
- ) ;
170
-
171
- $rootScope . $apply ( callback ) ;
172
-
173
- }
174
- ) ;
175
-
176
-
177
- } ;
178
-
179
- }
180
- ) ;
181
-
182
-
183
- // -------------------------------------------------- //
184
- // -------------------------------------------------- //
185
-
186
-
187
143
// I control the root of the application.
188
144
app . controller (
189
145
"AppController" ,
190
- function ( $scope ) {
191
-
192
- // Since this Controller will be instantiated once
193
- // the application is bootstrapped, let's log it to
194
- // the console so we can see the timing.
195
- console . log ( "Controller instantiated (after bootstrap)." ) ;
146
+ function ( $scope , withLazyModule ) {
196
147
197
148
// I determine which view is rendered.
198
149
$scope . subview = "before" ;
@@ -208,14 +159,28 @@ <h1>
208
159
209
160
if ( $scope . subview === "before" ) {
210
161
211
- $scope . withLazyModule (
162
+ // Once the "lazy" module has been loaded,
163
+ // then show the corresponding view.
164
+ withLazyModule (
212
165
function ( ) {
213
166
214
167
$scope . subview = "after" ;
215
168
216
169
}
217
170
) ;
218
171
172
+ // The lazy-load of the module returns a
173
+ // promise. This is here just to demonstrate
174
+ // that multiple bindings can listen for the
175
+ // resolution or rejection of the lazy module.
176
+ withLazyModule ( ) . then (
177
+ function ( ) {
178
+
179
+ console . log ( "Lazy module loaded." ) ;
180
+
181
+ }
182
+ ) ;
183
+
219
184
} else {
220
185
221
186
$scope . subview = "before" ;
@@ -228,6 +193,102 @@ <h1>
228
193
) ;
229
194
230
195
196
+ // -------------------------------------------------- //
197
+ // -------------------------------------------------- //
198
+
199
+
200
+ // I load the "Lazy" module and resolve the returned Promise
201
+ // when the components and the relevant templates have been
202
+ // loaded.
203
+ app . factory (
204
+ "withLazyModule" ,
205
+ function ( $rootScope , $templateCache , $q ) {
206
+
207
+ var deferred = $q . defer ( ) ;
208
+ var promise = null ;
209
+
210
+ function loadModule ( successCallback , errorCallback ) {
211
+
212
+ successCallback = ( successCallback || angular . noop ) ;
213
+ errorCallback = ( errorCallback || angular . noop ) ;
214
+
215
+ // If the module has already been loaded then
216
+ // simply bind the handlers to the existing promise.
217
+ // No need to try and load the files again.
218
+ if ( promise ) {
219
+
220
+ return (
221
+ promise . then ( successCallback , errorCallback )
222
+ ) ;
223
+
224
+ }
225
+
226
+ promise = deferred . promise ;
227
+
228
+ // Wire the callbacks into the deferred outcome.
229
+ promise . then ( successCallback , errorCallback ) ;
230
+
231
+ // Load the module templates and components.
232
+ // --
233
+ // The first dependency here is an HTML file which
234
+ // is loaded using the text! plugin. This will pass
235
+ // the value through as an HTML string.
236
+ require (
237
+ [
238
+ "../../vendor/require/text!lazy.htm" ,
239
+ "lazy.js"
240
+ ] ,
241
+ function requrieSuccess ( templatesHtml ) {
242
+
243
+ // Fill the template cache. The file content
244
+ // is expected to be a list of top level
245
+ // Script tags.
246
+ $ ( templatesHtml ) . each (
247
+ function ( ) {
248
+
249
+ var template = $ ( this ) ;
250
+ var id = template . attr ( "id" ) ;
251
+ var content = template . html ( ) ;
252
+
253
+ $templateCache . put ( id , content ) ;
254
+
255
+ }
256
+ ) ;
257
+
258
+ // Module loaded, resolve deferred.
259
+ $rootScope . $apply (
260
+ function ( ) {
261
+
262
+ deferred . resolve ( ) ;
263
+
264
+ }
265
+ ) ;
266
+
267
+ } ,
268
+ function requireError ( error ) {
269
+
270
+ // Module load failed, reject deferred.
271
+ $rootScope . $apply (
272
+ function ( ) {
273
+
274
+ deferred . reject ( error ) ;
275
+
276
+ }
277
+ ) ;
278
+
279
+ }
280
+ ) ;
281
+
282
+ return ( promise ) ;
283
+
284
+ }
285
+
286
+ return ( loadModule ) ;
287
+
288
+ }
289
+ ) ;
290
+
291
+
231
292
</ script >
232
293
233
294
</ body >
0 commit comments