Debian Codegraph @

dojox/mvc/_atBindingMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/lang",
4 "dojo/_base/declare",
5 "dojo/has",
6 "dojo/Stateful",
7 "./resolve",
8 "./sync"
9 ], function(array, lang, declare, has, Stateful, resolve, sync){
10 var getLogContent = (has("mvc-bindings-log-api")) ? function(/*dojo/Stateful*/ target, /*String*/ targetProp){
11 return [target._setIdAttr || !target.declaredClass ? target : target.declaredClass, targetProp].join(":");
12 } : "";
13
14 var logResolveFailure = (has("mvc-bindings-log-api")) ? function(target, targetProp){
15 console.warn(targetProp + " could not be resolved" + (typeof target == "string" ? (" with " + target) : "") + ".");
16 } : "";
17
18 function getParent(/*dijit/_WidgetBase*/ w){
19 // summary:
20 // Returns parent widget having data binding target for relative data binding.
21 // w: dijit/_WidgetBase
22 // The widget.
23
24 // Usage of dijit/registry module is optional. Return null if it's not already loaded.
25 var registry;
26 try{
27 registry = require("dijit/registry");
28 }catch(e){
29 return;
30 }
31 var pn = w.domNode && w.domNode.parentNode, pw, pb;
32 while(pn){
33 pw = registry.getEnclosingWidget(pn);
34 if(pw){
35 var relTargetProp = pw._relTargetProp || "target", pt = lang.isFunction(pw.get) ? pw.get(relTargetProp) : pw[relTargetProp];
36 if(pt || relTargetProp in pw.constructor.prototype){
37 return pw; // dijit/_WidgetBase
38 }
39 }
40 pn = pw && pw.domNode.parentNode;
41 }
42 }
43
44 function bind(/*dojo/Stateful|String*/ source, /*String*/ sourceProp, /*dijit/_WidgetBase*/ target, /*String*/ targetProp, /*dojox/mvc/sync.options*/ options){
45 // summary:
46 // Resolves the data binding literal, and starts data binding.
47 // source: dojo/Stateful|String
48 // Source data binding literal or dojo/Stateful to be synchronized.
49 // sourceProp: String
50 // The property name in source to be synchronized.
51 // target: dijit/_WidgetBase
52 // Target dojo/Stateful to be synchronized.
53 // targetProp: String
54 // The property name in target to be synchronized.
55 // options: dojox/mvc/sync.options
56 // Data binding options.
57
58 var _handles = {}, parent = getParent(target), relTargetProp = parent && parent._relTargetProp || "target";
59
60 function resolveAndBind(){
61 _handles["Two"] && _handles["Two"].unwatch();
62 delete _handles["Two"];
63
64 var relTarget = parent && (lang.isFunction(parent.get) ? parent.get(relTargetProp) : parent[relTargetProp]),
65 resolvedSource = resolve(source, relTarget),
66 resolvedTarget = resolve(target, relTarget);
67
68 if(has("mvc-bindings-log-api") && (!resolvedSource || /^rel:/.test(source) && !parent)){ logResolveFailure(source, sourceProp); }
69 if(has("mvc-bindings-log-api") && (!resolvedTarget || /^rel:/.test(target) && !parent)){ logResolveFailure(target, targetProp); }
70 if(!resolvedSource || !resolvedTarget || (/^rel:/.test(source) || /^rel:/.test(target)) && !parent){ return; }
71 if((!resolvedSource.set || !resolvedSource.watch) && sourceProp == "*"){
72 if(has("mvc-bindings-log-api")){ logResolveFailure(source, sourceProp); }
73 return;
74 }
75
76 if(sourceProp == null){
77 // If source property is not specified, it means this handle is just for resolving data binding target.
78 // (For dojox/mvc/Group and dojox/mvc/Repeat)
79 // Do not perform data binding synchronization in such case.
80 lang.isFunction(resolvedTarget.set) ? resolvedTarget.set(targetProp, resolvedSource) : (resolvedTarget[targetProp] = resolvedSource);
81 if(has("mvc-bindings-log-api")){
82 console.log("dojox/mvc/_atBindingMixin set " + resolvedSource + " to: " + getLogContent(resolvedTarget, targetProp));
83 }
84 }else{
85 // Start data binding
86 _handles["Two"] = sync(resolvedSource, sourceProp, resolvedTarget, targetProp, options); // dojox/mvc/sync.handle
87 }
88 }
89
90 resolveAndBind();
91 if(parent && /^rel:/.test(source) || /^rel:/.test(target) && lang.isFunction(parent.set) && lang.isFunction(parent.watch)){
92 _handles["rel"] = parent.watch(relTargetProp, function(name, old, current){
93 if(old !== current){
94 if(has("mvc-bindings-log-api")){ console.log("Change in relative data binding target: " + parent); }
95 resolveAndBind();
96 }
97 });
98 }
99 var h = {};
100 h.unwatch = h.remove = function(){
101 for(var s in _handles){
102 _handles[s] && _handles[s].unwatch();
103 delete _handles[s];
104 }
105 };
106 return h;
107 }
108
109 var mixin = {
110 // summary:
111 // The mixin for dijit/_WidgetBase to support data binding.
112
113 // dataBindAttr: String
114 // The attribute name for data binding.
115 dataBindAttr: "data-mvc-bindings",
116
117 _dbpostscript: function(/*Object?*/ params, /*DomNode|String*/ srcNodeRef){
118 // summary:
119 // See if any parameters for this widget are dojox/mvc/at handles.
120 // If so, move them under this._refs to prevent widget implementations from referring them.
121
122 var refs = this._refs = (params || {}).refs || {};
123 for(var prop in params){
124 if((params[prop] || {}).atsignature == "dojox.mvc.at"){
125 var h = params[prop];
126 delete params[prop];
127 refs[prop] = h;
128 }
129 }
130
131 var dbParams = new Stateful(),
132 _self = this;
133 dbParams.toString = function(){ return '[Mixin value of widget ' + _self.declaredClass + ', ' + (_self.id || 'NO ID') + ']'; };
134 dbParams.canConvertToLoggable = true;
135 this._startAtWatchHandles(dbParams);
136 for(var prop in refs){
137 if(dbParams[prop] !== void 0){
138 (params = params || {})[prop] = dbParams[prop];
139 }
140 }
141 this._stopAtWatchHandles();
142 },
143
144 _startAtWatchHandles: function(/*dojo/Stateful*/ bindWith){
145 // summary:
146 // Establish data bindings based on dojox/mvc/at handles.
147 // bindWith: dojo/Stateful
148 // The dojo/Stateful to bind properties with.
149
150 this.canConvertToLoggable = true;
151
152 var refs = this._refs;
153 if(refs){
154 var atWatchHandles = this._atWatchHandles = this._atWatchHandles || {};
155
156 // Clear the cache of properties that data binding is established with
157 this._excludes = null;
158
159 // First, establish non-wildcard data bindings
160 for(var prop in refs){
161 if(!refs[prop] || prop == "*"){ continue; }
162 atWatchHandles[prop] = bind(refs[prop].target, refs[prop].targetProp, bindWith || this, prop, {bindDirection: refs[prop].bindDirection, converter: refs[prop].converter, equals: refs[prop].equalsCallback});
163 }
164
165 // Then establish wildcard data bindings
166 if((refs["*"] || {}).atsignature == "dojox.mvc.at"){
167 atWatchHandles["*"] = bind(refs["*"].target, refs["*"].targetProp, bindWith || this, "*", {bindDirection: refs["*"].bindDirection, converter: refs["*"].converter, equals: refs["*"].equalsCallback});
168 }
169 }
170 },
171
172 _stopAtWatchHandles: function(){
173 // summary:
174 // Stops data binding synchronization handles as widget is destroyed.
175
176 for(var s in this._atWatchHandles){
177 this._atWatchHandles[s].unwatch();
178 delete this._atWatchHandles[s];
179 }
180 },
181
182 _setAtWatchHandle: function(/*String*/ name, /*Anything*/ value){
183 // summary:
184 // Called if the value is a dojox/mvc/at handle.
185 // If this widget has started, start data binding with the new dojox/mvc/at handle.
186 // Otherwise, queue it up to this._refs so that _dbstartup() can pick it up.
187
188 if(name == "ref"){
189 throw new Error(this + ": 1.7 ref syntax used in conjunction with 1.8 dojox/mvc/at syntax, which is not supported.");
190 }
191
192 // Claen up older data binding
193 var atWatchHandles = this._atWatchHandles = this._atWatchHandles || {};
194 if(atWatchHandles[name]){
195 atWatchHandles[name].unwatch();
196 delete atWatchHandles[name];
197 }
198
199 // Claar the value
200 this[name] = null;
201
202 // Clear the cache of properties that data binding is established with
203 this._excludes = null;
204
205 if(this._started){
206 // If this widget has been started already, establish data binding immediately.
207 atWatchHandles[name] = bind(value.target, value.targetProp, this, name, {bindDirection: value.bindDirection, converter: value.converter, equals: value.equalsCallback});
208 }else{
209 // Otherwise, queue it up to this._refs so that _dbstartup() can pick it up.
210 this._refs[name] = value;
211 }
212 },
213
214 _setBind: function(/*Object*/ value){
215 // summary:
216 // Sets data binding described in data-mvc-bindings.
217
218 var list = eval("({" + value + "})");
219 for(var prop in list){
220 var h = list[prop];
221 if((h || {}).atsignature != "dojox.mvc.at"){
222 console.warn(prop + " in " + dataBindAttr + " is not a data binding handle.");
223 }else{
224 this._setAtWatchHandle(prop, h);
225 }
226 }
227 },
228
229 _getExcludesAttr: function(){
230 // summary:
231 // Returns list of all properties that data binding is established with.
232
233 if(this._excludes){
234 return this._excludes; // String[]
235 }
236 var list = [];
237 for(var s in this._atWatchHandles){
238 if(s != "*"){ list.push(s); }
239 }
240 return list; // String[]
241 },
242
243 _getPropertiesAttr: function(){
244 // summary:
245 // Returns list of all properties in this widget, except "id".
246 // returns: String[]
247 // The list of all properties in this widget, except "id"..
248
249 if(this.constructor._attribs){
250 return this.constructor._attribs; // String[]
251 }
252 var list = ["onClick"].concat(this.constructor._setterAttrs);
253 array.forEach(["id", "excludes", "properties", "ref", "binding"], function(s){
254 var index = array.indexOf(list, s);
255 if(index >= 0){ list.splice(index, 1); }
256 });
257 return this.constructor._attribs = list; // String[]
258 }
259 };
260
261 mixin[mixin.dataBindAttr] = ""; // Let parser treat the attribute as string
262
263 var _atBindingMixin = declare("dojox/mvc/_atBindingMixin", null, mixin);
264 _atBindingMixin.mixin = mixin; // Keep the plain object version
265 return _atBindingMixin;
266 });