1 | define(["require", "dojo/when", "dojo/on", "dojo/_base/declare", "dojo/_base/lang", "dojo/Deferred", |
---|
2 | "dijit/Destroyable", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin", "./ViewBase", "./utils/nls"], |
---|
3 | function(require, when, on, declare, lang, Deferred, Destroyable, _TemplatedMixin, _WidgetsInTemplateMixin, ViewBase, nls){ |
---|
4 | |
---|
5 | return declare("dojox.app.View", [_TemplatedMixin, _WidgetsInTemplateMixin, Destroyable, ViewBase], { |
---|
6 | // summary: |
---|
7 | // View class inheriting from ViewBase adding templating & globalization capabilities. |
---|
8 | constructor: function(params){ |
---|
9 | // summary: |
---|
10 | // Constructs a View instance either from a configuration or programmatically. |
---|
11 | // |
---|
12 | // example: |
---|
13 | // | use configuration file |
---|
14 | // | |
---|
15 | // | // load view controller from views/simple.js by default |
---|
16 | // | "simple":{ |
---|
17 | // | "template": "myapp/views/simple.html", |
---|
18 | // | "nls": "myapp/nls/simple" |
---|
19 | // | "dependencies":["dojox/mobile/TextBox"] |
---|
20 | // | } |
---|
21 | // | |
---|
22 | // | "home":{ |
---|
23 | // | "template": "myapp/views/home.html", // no controller set to not use a view controller |
---|
24 | // | "dependencies":["dojox/mobile/TextBox"] |
---|
25 | // | } |
---|
26 | // | "main":{ |
---|
27 | // | "template": "myapp/views/main.html", |
---|
28 | // | "controller": "myapp/views/main.js", // identify load view controller from views/main.js |
---|
29 | // | "dependencies":["dojox/mobile/TextBox"] |
---|
30 | // | } |
---|
31 | // |
---|
32 | // example: |
---|
33 | // | var viewObj = new View({ |
---|
34 | // | app: this.app, |
---|
35 | // | id: this.id, |
---|
36 | // | name: this.name, |
---|
37 | // | parent: this, |
---|
38 | // | templateString: this.templateString, |
---|
39 | // | template: this.template, |
---|
40 | // | controller: this.controller |
---|
41 | // | }); |
---|
42 | // | viewObj.start(); // start view |
---|
43 | // |
---|
44 | // params: |
---|
45 | // view parameters, include: |
---|
46 | // |
---|
47 | // - app: the app |
---|
48 | // - id: view id |
---|
49 | // - name: view name |
---|
50 | // - template: view template identifier. If templateString is not empty, this parameter is ignored. |
---|
51 | // - templateString: view template string |
---|
52 | // - controller: view controller module identifier |
---|
53 | // - parent: parent view |
---|
54 | // - children: children views |
---|
55 | // - nls: nls definition module identifier |
---|
56 | }, |
---|
57 | |
---|
58 | // _TemplatedMixin requires a connect method if data-dojo-attach-* are used |
---|
59 | connect: function(obj, event, method){ |
---|
60 | return this.own(on(obj, event, lang.hitch(this, method)))[0]; // handle |
---|
61 | }, |
---|
62 | |
---|
63 | _loadTemplate: function(){ |
---|
64 | // summary: |
---|
65 | // load view HTML template and dependencies. |
---|
66 | // tags: |
---|
67 | // private |
---|
68 | // |
---|
69 | |
---|
70 | if(this.templateString){ |
---|
71 | return true; |
---|
72 | }else{ |
---|
73 | var tpl = this.template; |
---|
74 | var deps = this.dependencies?this.dependencies:[]; |
---|
75 | if(tpl){ |
---|
76 | if(tpl.indexOf("./") == 0){ |
---|
77 | tpl = "app/"+tpl; |
---|
78 | } |
---|
79 | deps = deps.concat(["dojo/text!"+tpl]); |
---|
80 | } |
---|
81 | var def = new Deferred(); |
---|
82 | if(deps.length > 0){ |
---|
83 | var requireSignal; |
---|
84 | try{ |
---|
85 | requireSignal = require.on("error", lang.hitch(this, function(error){ |
---|
86 | if(def.isResolved() || def.isRejected()){ |
---|
87 | return; |
---|
88 | } |
---|
89 | if(error.info[0] && error.info[0].indexOf(this.template) >= 0 ){ |
---|
90 | def.resolve(false); |
---|
91 | requireSignal.remove(); |
---|
92 | } |
---|
93 | })); |
---|
94 | require(deps, function(){ |
---|
95 | def.resolve.call(def, arguments); |
---|
96 | requireSignal.remove(); |
---|
97 | }); |
---|
98 | }catch(e){ |
---|
99 | def.resolve(false); |
---|
100 | if(requireSignal){ |
---|
101 | requireSignal.remove(); |
---|
102 | } |
---|
103 | } |
---|
104 | }else{ |
---|
105 | def.resolve(true); |
---|
106 | } |
---|
107 | var loadViewDeferred = new Deferred(); |
---|
108 | when(def, lang.hitch(this, function(){ |
---|
109 | this.templateString = this.template ? arguments[0][arguments[0].length - 1] : "<div></div>"; |
---|
110 | loadViewDeferred.resolve(this); |
---|
111 | })); |
---|
112 | return loadViewDeferred; |
---|
113 | } |
---|
114 | }, |
---|
115 | |
---|
116 | // start view |
---|
117 | load: function(){ |
---|
118 | var tplDef = new Deferred(); |
---|
119 | var defDef = this.inherited(arguments); |
---|
120 | var nlsDef = nls(this); |
---|
121 | // when parent loading is done (controller), proceed with template |
---|
122 | // (for data-dojo-* to work we need to wait for controller to be here, this is also |
---|
123 | // useful when the controller is used as a layer for the view) |
---|
124 | when(defDef, lang.hitch(this, function(){ |
---|
125 | when(nlsDef, lang.hitch(this, function(nls){ |
---|
126 | // we inherit from the parent NLS |
---|
127 | this.nls = lang.mixin({}, this.parent.nls); |
---|
128 | if(nls){ |
---|
129 | // make sure template can access nls doing ${nls.myprop} |
---|
130 | lang.mixin(this.nls, nls); |
---|
131 | } |
---|
132 | when(this._loadTemplate(), function(value){ |
---|
133 | tplDef.resolve(value); |
---|
134 | }); |
---|
135 | })); |
---|
136 | })); |
---|
137 | return tplDef; |
---|
138 | }, |
---|
139 | |
---|
140 | _startup: function(){ |
---|
141 | // summary: |
---|
142 | // startup widgets in view template. |
---|
143 | // tags: |
---|
144 | // private |
---|
145 | this.buildRendering(); |
---|
146 | this.inherited(arguments); |
---|
147 | } |
---|
148 | }); |
---|
149 | }); |
---|