forked from dmachi/dojox_application
/
main.js
252 lines (230 loc) · 7.21 KB
/
main.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Deferred", "dojo/when", "dojo/on", "dojo/ready", "dojo/_base/window", "dojo/dom-construct", "./model", "./View", "./controllers/Load", "./controllers/Transition", "./controllers/Layout", "dojo/_base/loader", "dojo/store/Memory"],
function(lang, declare, Deferred, when, on, ready, baseWindow, dom, Model, View, LoadController, TransitionController, LayoutController){
dojo.experimental("dojox.app");
var Application = declare(null, {
constructor: function(params, node){
lang.mixin(this, params);
this.params = params;
this.id = params.id;
this.defaultView = params.defaultView;
this.widgetId = params.id;
this.controllers = [];
this.children = {};
this.loadedModels = {};
// Create a new domNode and append to body
// Need to bind startTransition event on application domNode,
// Because dojox.mobile.ViewController bind startTransition event on document.body
this.domNode = dom.create("div", {
id: this.id,
style: "width:100%; height:100%"
});
node.appendChild(this.domNode);
},
createDataStore: function(params){
// summary:
// Create data store instance
//
// params: Object
// data stores configuration.
if(params.stores){
//create stores in the configuration.
for(var item in params.stores){
if(item.charAt(0) !== "_"){//skip the private properties
var type = params.stores[item].type ? params.stores[item].type : "dojo.store.Memory";
var config = {};
if(params.stores[item].params){
lang.mixin(config, params.stores[item].params);
}
var storeCtor = lang.getObject(type);
if(config.data && lang.isString(config.data)){
//get the object specified by string value of data property
//cannot assign object literal or reference to data property
//because json.ref will generate __parent to point to its parent
//and will cause infinitive loop when creating StatefulModel.
config.data = lang.getObject(config.data);
}
params.stores[item].store = new storeCtor(config);
}
}
}
},
createControllers: function(controllers){
// summary:
// Create controller instance
//
// parent: Array
// controller configuration array.
// returns:
// controllerDeferred object
if(controllers){
var requireItems = [];
for(var i = 0; i < controllers.length; i++){
requireItems.push(controllers[i]);
}
var def = new Deferred();
var requireSignal;
try{
requireSignal = require.on("error", function(error){
if(def.fired != -1){
return;
}
def.reject("load controllers error.");
requireSignal.remove();
});
require(requireItems, function(){
def.resolve.call(def, arguments);
requireSignal.remove();
});
}catch(ex){
def.reject("load controllers error.");
requireSignal.remove();
}
var controllerDef = new Deferred();
when(def, lang.hitch(this, function(){
for(var i = 0; i < arguments[0].length; i++){
// Store Application object on each controller.
this.controllers.push(new arguments[0][i](this));
}
controllerDef.resolve(this);
}), function(){
//require def error, reject loadChildDeferred
controllerDef.reject("load controllers error.");
});
return controllerDef;
}
},
trigger: function(event, params){
// summary:
// trigger an event
//
// event: String
// event name. The event is binded by controller.bind() method.
// params: Object
// event parameters.
on.emit(this.domNode, event, params);
},
// load default view and setupControllers and startup the default view
start: function(){
//create application level data store
this.createDataStore(this.params);
// create application template view
if(this.template){
this.view = new View({
id: this.id,
name: this.name,
models: this.models,
parent: this,
templateString: this.templateString,
definition: this.definition
});
when(this.view.start(), lang.hitch(this, function(){
this.domNode = this.view.domNode;
this.setupControllers();
}));
}else{
this.setupControllers();
}
},
setupControllers: function(){
// create application controller instance
new LoadController(this);
new TransitionController(this);
new LayoutController(this);
// move set _startView operation from history module to application
var hash = window.location.hash;
this._startView = ((hash && hash.charAt(0) == "#") ? hash.substr(1) : hash) || this.defaultView;
// create application level data model
var loadModelLoaderDeferred = new Deferred();
var createPromise;
try{
createPromise = Model(this.params.models, this);
}catch(ex){
loadModelLoaderDeferred.reject("load model error.");
return loadModelLoaderDeferred.promise;
}
if(createPromise.then){
when(createPromise, lang.hitch(this, function(newModel){
this.loadedModels = newModel;
this.startup();
}),
function(){
loadModelLoaderDeferred.reject("load model error.")
});
}else{
this.loadedModels = createPromise;
this.startup();
}
},
startup: function(){
// load controllers in configuration file
var controllers = this.createControllers(this.params.controllers);
when(controllers, lang.hitch(this, function(result){
// emit load event and let controller to load view.
this.trigger("load", {
"viewId": this.defaultView,
"callback": lang.hitch(this, function(){
var selectId = this.defaultView.split(",");
selectId = selectId.shift();
this.selectedChild = this.children[this.id + '_' + selectId];
if(this._startView !== this.defaultView){
this.trigger("transition", {
"viewId": this._startView
});
}else{
this.trigger("layout", {
"view": this
});
}
this.setStatus(this.lifecycle.STARTED);
})
});
}));
}
});
function generateApp(config, node, appSchema, validate){
// Register application module path
var path = window.location.pathname;
if(path.charAt(path.length) != "/"){
path = path.split("/");
path.pop();
path = path.join("/");
}
dojo.registerModulePath("app", path);
var modules = config.modules.concat(config.dependencies);
if(config.template){
modules.push("dojo/text!" + "app/" + config.template);
}
require(modules, function(){
var modules = [Application];
for(var i = 0; i < config.modules.length; i++){
modules.push(arguments[i]);
}
if(config.template){
var ext = {
templateString: arguments[arguments.length - 1]
}
}
App = declare(modules, ext);
ready(function(){
app = App(config, node || baseWindow.body());
app.setStatus(app.lifecycle.STARTING);
app.start();
});
});
}
return function(config, node){
if(!config){
throw Error("App Config Missing");
}
if(config.validate){
require(["dojox/json/schema", "dojox/json/ref", "dojo/text!dojox/application/schema/application.json"], function(schema, appSchema){
schema = dojox.json.ref.resolveJson(schema);
if(schema.validate(config, appSchema)){
generateApp(config, node);
}
});
}else{
generateApp(config, node);
}
}
});