-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (62 loc) · 2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*== Angular app definition ==*/
(function(window, document, angular)
{
'use strict';
var PokeQuizz = angular.module('PokeQuizz', ['ngRoute', 'i18n', 'PokeApi', 'Pokemon']),
loadedControllers = {};
PokeQuizz
.config([
'$compileProvider',
function($compileProvider)
{
$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|file):|data:image\//);
}
])
.config([
'$routeProvider',
'$controllerProvider',
function($routeProvider, $controllerProvider)
{
var routes = ['home', 'game', 'end'];
//Exposte $controllerProvider for units testings
PokeQuizz.$controllerProvider = $controllerProvider;
for(var i = 0, l = routes.length; i < l; i++)
{
(function(routeName)
{
$routeProvider.when('/' + routeName,
{
templateUrl: './views/' + routeName + '.html',
controller: routeName + 'Ctrl',
//Handle preloading
resolve: {
preload: function($q)
{
//If controller already loaded, no need to reload it
if( ! loadedControllers[routeName])
{
var deferred = $q.defer(),
script = document.createElement('script');
script.onload = function()
{
loadedControllers[routeName] = true;
//Call function we've just loaded: it contains the new controller definition
var ctrlNameProvider = '_controller_' + routeName + 'Ctrl';
window[ctrlNameProvider]($controllerProvider);
delete window[ctrlNameProvider];
//All is ready, we can tell AngularJS it can proceeds routing
deferred.resolve();
};
script.src = './controllers/' + routeName + '.js';
document.body.appendChild(script);
return deferred.promise;
}
}
}
});
})(routes[i]);
}
$routeProvider.otherwise({redirectTo: '/home'});
}]);
window.PokeQuizz = PokeQuizz;
})(window, document, angular);