Debian Codegraph @

dojox/app/ViewBase.js

raw symbols SCIP
1 define(["require", "dojo/when", "dojo/on", "dojo/dom-attr", "dojo/dom-style", "dojo/_base/declare", "dojo/_base/lang",
2 "dojo/Deferred", "./utils/model", "./utils/constraints"],
3 function(require, when, on, domAttr, domStyle, declare, lang, Deferred, model, constraints){
4 return declare("dojox.app.ViewBase", null, {
5 // summary:
6 // View base class with model & controller capabilities. Subclass must implement rendering capabilities.
7 constructor: function(params){
8 // summary:
9 // Constructs a ViewBase instance.
10 // params:
11 // view parameters, include:
12 //
13 // - app: the app
14 // - id: view id
15 // - name: view name
16 // - parent: parent view
17 // - controller: view controller module identifier
18 // - children: children views
19 this.id = "";
20 this.name = "";
21 this.children = {};
22 this.selectedChildren = {};
23 this.loadedStores = {};
24 // private
25 this._started = false;
26 lang.mixin(this, params);
27 // mixin views configuration to current view instance.
28 if(this.parent.views){
29 lang.mixin(this, this.parent.views[this.name]);
30 }
31 },
32
33 // start view
34 start: function(){
35 // summary:
36 // start view object.
37 // load view template, view controller implement and startup all widgets in view template.
38 if(this._started){
39 return this;
40 }
41 this._startDef = new Deferred();
42 when(this.load(), lang.hitch(this, function(){
43 // call setupModel, after setupModel startup will be called after startup the loadViewDeferred will be resolved
44 this._createDataStore(this);
45 this._setupModel();
46 }));
47 return this._startDef;
48 },
49
50 load: function(){
51 var vcDef = this._loadViewController();
52 when(vcDef, lang.hitch(this, function(controller){
53 if(controller){
54 //lang.mixin(this, controller);
55 declare.safeMixin(this, controller);
56 }
57 }));
58 return vcDef;
59 },
60
61 _createDataStore: function(){
62 // summary:
63 // Create data store instance for View specific stores
64 //
65 // TODO: move this into a common place for use by main and ViewBase
66 //
67 if(this.parent.loadedStores){
68 lang.mixin(this.loadedStores, this.parent.loadedStores);
69 }
70
71 if(this.stores){
72 //create stores in the configuration.
73 for(var item in this.stores){
74 if(item.charAt(0) !== "_"){//skip the private properties
75 var type = this.stores[item].type ? this.stores[item].type : "dojo/store/Memory";
76 var config = {};
77 if(this.stores[item].params){
78 lang.mixin(config, this.stores[item].params);
79 }
80 // we assume the store is here through dependencies
81 try{
82 var storeCtor = require(type);
83 }catch(e){
84 throw new Error(type+" must be listed in the dependencies");
85 }
86 if(config.data && lang.isString(config.data)){
87 //get the object specified by string value of data property
88 //cannot assign object literal or reference to data property
89 //because json.ref will generate __parent to point to its parent
90 //and will cause infinitive loop when creating StatefulModel.
91 config.data = lang.getObject(config.data);
92 }
93 if(this.stores[item].observable){
94 try{
95 var observableCtor = require("dojo/store/Observable");
96 }catch(e){
97 throw new Error("dojo/store/Observable must be listed in the dependencies");
98 }
99 this.stores[item].store = observableCtor(new storeCtor(config));
100 }else{
101 this.stores[item].store = new storeCtor(config);
102 }
103 this.loadedStores[item] = this.stores[item].store; // add this store to loadedStores for the view
104 }
105 }
106 }
107 },
108
109 _setupModel: function(){
110 // summary:
111 // Load views model if it is not already loaded then call _startup.
112 // tags:
113 // private
114
115 if(!this.loadedModels){
116 var createPromise;
117 try{
118 createPromise = model(this.models, this.parent, this.app);
119 }catch(e){
120 throw new Error("Error creating models: "+e.message);
121 }
122 when(createPromise, lang.hitch(this, function(models){
123 if(models){
124 // if models is an array it comes from dojo/promise/all. Each array slot contains the same result object
125 // so pick slot 0.
126 this.loadedModels = lang.isArray(models)?models[0]:models;
127 }
128 this._startup();
129 }),
130 function(err){
131 throw new Error("Error creating models: "+err.message);
132 });
133 }else{ // loadedModels already created so call _startup
134 this._startup();
135 }
136 },
137
138 _startup: function(){
139 // summary:
140 // startup widgets in view template.
141 // tags:
142 // private
143
144 this._initViewHidden();
145 this._needsResize = true; // flag used to be sure resize has been called before transition
146
147 this._startLayout();
148 },
149
150 _initViewHidden: function(){
151 domStyle.set(this.domNode, "visibility", "hidden");
152 },
153
154 _startLayout: function(){
155 // summary:
156 // startup widgets in view template.
157 // tags:
158 // private
159 this.app.log(" > in app/ViewBase _startLayout firing layout for name=[",this.name,"], parent.name=[",this.parent.name,"]");
160
161 if(!this.hasOwnProperty("constraint")){
162 this.constraint = domAttr.get(this.domNode, "data-app-constraint") || "center";
163 }
164 constraints.register(this.constraint);
165
166
167 this.app.emit("app-initLayout", {
168 "view": this,
169 "callback": lang.hitch(this, function(){
170 //start widget
171 this.startup();
172
173 // call view assistant's init() method to initialize view
174 this.app.log(" > in app/ViewBase calling init() name=[",this.name,"], parent.name=[",this.parent.name,"]");
175 this.init();
176 this._started = true;
177 if(this._startDef){
178 this._startDef.resolve(this);
179 }
180 })
181 });
182 },
183
184
185 _loadViewController: function(){
186 // summary:
187 // Load view controller by configuration or by default.
188 // tags:
189 // private
190 //
191 var viewControllerDef = new Deferred();
192 var path;
193
194 if(!this.controller){ // no longer using this.controller === "none", if we dont have one it means none.
195 this.app.log(" > in app/ViewBase _loadViewController no controller set for view name=[",this.name,"], parent.name=[",this.parent.name,"]");
196 viewControllerDef.resolve(true);
197 return viewControllerDef;
198 }else{
199 path = this.controller.replace(/(\.js)$/, "");
200 }
201
202 var requireSignal;
203 try{
204 var loadFile = path;
205 var index = loadFile.indexOf("./");
206 if(index >= 0){
207 loadFile = path.substring(index+2);
208 }
209 requireSignal = require.on ? require.on("error", function(error){
210 if(viewControllerDef.isResolved() || viewControllerDef.isRejected()){
211 return;
212 }
213 if(error.info[0] && (error.info[0].indexOf(loadFile) >= 0)){
214 viewControllerDef.resolve(false);
215 if(requireSignal){
216 requireSignal.remove();
217 }
218 }
219 }) : null;
220
221 if(path.indexOf("./") == 0){
222 path = "app/"+path;
223 }
224
225 require([path], function(controller){
226 viewControllerDef.resolve(controller);
227 if(requireSignal){
228 requireSignal.remove();
229 }
230 });
231 }catch(e){
232 viewControllerDef.reject(e);
233 if(requireSignal){
234 requireSignal.remove();
235 }
236 }
237 return viewControllerDef;
238 },
239
240 init: function(){
241 // summary:
242 // view life cycle init()
243 },
244
245 beforeActivate: function(){
246 // summary:
247 // view life cycle beforeActivate()
248 },
249
250 afterActivate: function(){
251 // summary:
252 // view life cycle afterActivate()
253 },
254
255 beforeDeactivate: function(){
256 // summary:
257 // view life cycle beforeDeactivate()
258 },
259
260 afterDeactivate: function(){
261 // summary:
262 // view life cycle afterDeactivate()
263 },
264
265 destroy: function(){
266 // summary:
267 // view life cycle destroy()
268 }
269 });
270 });