This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (104 loc) · 3.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var myModule = angular.module('myModule', []);
myModule.factory('init', function ($q, $rootScope, $browser) {
var initFunctions = [
'controllerWithInitialization'
];
var registeredInitFunctions = {};
var initialized = false;
var initApplication = function () {
var controllerWithInitialization = registeredInitFunctions['controllerWithInitialization'];
var broadcastAppInitialized = function () {
$browser.defer(function () {
initialized = true;
$rootScope.$apply(function () {
$rootScope.$broadcast('appInitialized');
});
});
};
controllerWithInitialization.init()
.then(broadcastAppInitialized);
};
$rootScope.$on('$routeChangeStart', function () {
registeredInitFunctions = {};
initialized = false;
});
var initAppWhenReady = function () {
var registeredInitFunctionNames = _.keys(registeredInitFunctions);
var isRegistered = _.partial(_.contains, registeredInitFunctionNames);
if (_.every(initFunctions, isRegistered)) {
initApplication();
registeredInitFunctions = null;
}
};
var init = function (name, dependencies, initCallback) {
registeredInitFunctions[name] = {
init: function () {
var internalDependencies = $q.all(dependencies);
return internalDependencies.then(initCallback);
}};
initAppWhenReady();
};
init.watchAfterInit = function (scope, expression, listener, deepEqual) {
scope.$watch(expression, function (newValue, oldValue, listenerScope) {
if (initialized) {
listener(newValue, oldValue, listenerScope);
}
}, deepEqual);
};
init.onAfterInit = function (scope, event, listener) {
scope.$on(event, function (event) {
if (initialized) {
listener(event);
}
});
};
return init;
});
myModule.service('myService', function ($http) {
$http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";
this.getOptions = function () {
return $http({
"method": "post",
"url": 'http://jsfiddle.net/echo/json/',
"data": "delay=3&json=" + encodeURI(JSON.stringify({
options: [
{name: 'entry1', value: 0},
{name: 'entry2', value: 1},
{name: 'entry3', value: 2}
]
}))
});
};
});
myModule.controller('simpleController', function ($scope, myService) {
$scope.selectedOption = null;
$scope.options = [];
$scope.logentries = [];
myService.getOptions().then(function (result) {
$scope.options = result.data.options;
$scope.selectedOption = 0;
});
$scope.$watch('selectedOption', function (newValue, oldValue) {
// handle selection change ...
console.log("selection: " + $scope.selectedOption);
$scope.logentries.push(($scope.logentries.length + 1) + " - selection: " + $scope.selectedOption);
});
});
myModule.controller('controllerWithInitialization', function ($scope, myService, init) {
$scope.selectedOption = null;
$scope.options = [];
$scope.logentries = [];
init('controllerWithInitialization', [myService.getOptions()], function (result) {
$scope.options = result[0].data.options;
$scope.selectedOption = 0;
});
init.watchAfterInit($scope, 'selectedOption', function (newValue, oldValue) {
// handle selection change ...
console.log("selection: " + $scope.selectedOption);
$scope.logentries.push(($scope.logentries.length + 1) + " - selection: " + $scope.selectedOption);
});
$scope.$on('appInitialized', function () {
console.log("appInitialized - selection: " + $scope.selectedOption);
$scope.logentries.push(($scope.logentries.length + 1) + " - appInitialized - selection: " + $scope.selectedOption);
});
});