-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseApp.js
More file actions
228 lines (174 loc) Β· 7.07 KB
/
BaseApp.js
File metadata and controls
228 lines (174 loc) Β· 7.07 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*globals define, app, require, console, window*/
define([
'lodash', 'backbone', // 'src/BaseObject', // requireJS strange issue, DO NOT DELETE!!!
'src/models/BaseModel',
'src/controllers/LayoutController',
'src/utils/loader/DynamicModuleLoader' // by default, dynamic loading
], function (
_, Backbone,
BaseObject,
// BaseModel, // requireJS strange issue, DO NOT DELETE!!!
LayoutController,
ModuleLoader) {
'use strict';
var BaseApp = BaseObject.extend({
initialize: function (options) {
options = options || {};
_.extend(this, options);
this.started = false;
if (!this.mode) throw new Error('app.mode not specified');
if (!this.name) throw new Error('app.name not specified');
if (!this.version) throw new Error('app.version not specified');
if (!_.isString(this.rootUrl)) throw new Error('app.rootUrl not specified');
if (!this.endpoint) throw new Error('app.endpoint not specified');
if (!_.isBoolean(this.reloadController))
throw new Error('app.reloadController not specified');
this.user = undefined;
this.meta = {
application: undefined,
endpoint: undefined
};
_.extend(this.meta, options.meta || {});
if (!this.meta.permissionsEndpoint) throw new Error('app.meta.permissionsEndpoint not specified');
if (!this.meta.usersEndpoint) throw new Error('app.meta.usersEndpoint not specified');
if (!this.meta.application) throw new Error('app.meta.application not specified');
if (!this.User) throw new Error('User class not specified');
this.controller = this.controller || undefined;
this.controllerModule = this.controllerModule || undefined;
this.el = this.el || 'body';
if (!this.menu.module || this.menu.module === 'none') {
this.menu = this.menuView = false;
} else {
this.menu.module = this.menu.module || 'src/models/MenuCollection';
}
//this.menuModule = false;
this.moduleLoader = this.moduleLoader || options.moduleLoader || new ModuleLoader();
_.bindAll(this, 'onhashchange');
this.reloadCounter = 0;
// manually force a reload on hash change
// to prevent memory leaks
// use router!
// window.onhashchange = this.onhashchange;
},
onhashchange: function () {
var self = this,
forceReload = false;
if (this.reloadController) {
forceReload = true;
} else if (this.reloadThreshold !== -1 &&
this.reloadCounter >= this.reloadThreshold) {
console.log('reload threshold reached, forcing redirect');
forceReload = true;
}
if (forceReload) {
this.stopController();
window.location.reload(true);
} else {
this.reloadCounter++;
this.loadController(function (controller) {
// controller.start();
self.startController(controller);
});
}
},
stopController: function () {
if (this.controller) {
this.controller.destroy();
this.controller = null;
}
},
startController: function (Controller) {
var controller;
this.stopController();
// if it's a class, intantiate it
// otherwise it's an already instiated controller
controller = (_.isFunction(Controller) ? new Controller() : Controller);
this.controller = controller;
this.started = true;
this.controller.start();
},
// loads the user permission
// the layoutController (read MenuCollection)
// and instantiates the controller according to the url
start: function (callback) {
var self = this;
this.started = false;
if (!this.meta.mock) {
// load permissions
this.loadUser(function (user) {
self.user = user;
});
}
self.loadLayoutController(function () {
Backbone.history.start();
// finally, load controller from the url
// self.loadController(callback);
});
},
loadLayoutController: function (callback) {
// init layout dependencies values
this.menuCollection = null;
this.listenTo(this, 'layoutDependenciesLoaded', this.onLayoutDependenciesLoaded);
if (this.menu && !this.menu.mock) {
var self = this;
require([this.menu.module], function (MenuCollection) {
self.menuCollection = new MenuCollection();
self.trigger('layoutDependenciesLoaded', callback);
});
} else {
this.trigger('layoutDependenciesLoaded', callback);
}
},
onLayoutDependenciesLoaded: function (callback) {
this.LayoutController = this.LayoutController || LayoutController;
// initialize layoutController
this.layoutController = this.layoutController || new this.LayoutController({
el: this.el,
menuCollection: this.menuCollection,
menuView: this.menuView
});
this.layoutController.start();
callback();
},
loadController: function (callback) {
var self = this;
// unload the previously loaded controller
if (this.controller) {
this.controller.destroy();
this.controller = null;
}
// got to load the controller form the url
this.moduleLoader.loadFromUrl(
// success handler
function (Controller) {
self.controller = new Controller();
self.started = true;
callback(self.controller);
},
// error
function (err) {
// modal error message, controller not found
console.log(err);
console.log('about to go to ' + app.rootUrl);
// window.location.href = app.rootUrl;
throw err;
});
},
loadUser: function (callback) {
var user = new this.User({
config: this.meta,
success: function (user) {
callback(user);
}
});
return user;
},
startLoading: function (message) {
this.layoutController.startLoading(message);
},
completeLoading: function (message) {
this.layoutController.completeLoading(message);
}
});
return BaseApp;
});