Debian Codegraph @

dojox/app/View.js

raw symbols SCIP
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 ? 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 if(requireSignal){
92 requireSignal.remove();
93 }
94 }
95 })) : null;
96 require(deps, function(){
97 def.resolve.call(def, arguments);
98 if(requireSignal){
99 requireSignal.remove();
100 }
101 });
102 }catch(e){
103 def.resolve(false);
104 if(requireSignal){
105 requireSignal.remove();
106 }
107 }
108 }else{
109 def.resolve(true);
110 }
111 var loadViewDeferred = new Deferred();
112 when(def, lang.hitch(this, function(){
113 this.templateString = this.template ? arguments[0][arguments[0].length - 1] : "<div></div>";
114 loadViewDeferred.resolve(this);
115 }));
116 return loadViewDeferred;
117 }
118 },
119
120 // start view
121 load: function(){
122 var tplDef = new Deferred();
123 var defDef = this.inherited(arguments);
124 var nlsDef = nls(this);
125 // when parent loading is done (controller), proceed with template
126 // (for data-dojo-* to work we need to wait for controller to be here, this is also
127 // useful when the controller is used as a layer for the view)
128 when(defDef, lang.hitch(this, function(){
129 when(nlsDef, lang.hitch(this, function(nls){
130 // we inherit from the parent NLS
131 this.nls = lang.mixin({}, this.parent.nls);
132 if(nls){
133 // make sure template can access nls doing ${nls.myprop}
134 lang.mixin(this.nls, nls);
135 }
136 when(this._loadTemplate(), function(value){
137 tplDef.resolve(value);
138 });
139 }));
140 }));
141 return tplDef;
142 },
143
144 _startup: function(){
145 // summary:
146 // startup widgets in view template.
147 // tags:
148 // private
149 this.buildRendering();
150 this.inherited(arguments);
151 }
152 });
153 });