Debian Codegraph @

dojox/mvc/sync.js

raw symbols SCIP
1 define([
2 "dojo/_base/lang",
3 "dojo/_base/config",
4 "dojo/_base/array",
5 "dojo/has"
6 ], function(lang, config, array, has){
7 var mvc = lang.getObject("dojox.mvc", true);
8 /*=====
9 mvc = {};
10 =====*/
11
12 /*=====
13 dojox.mvc.sync.converter = {
14 // summary:
15 // Class/object containing the converter functions used when the data goes between data binding source (e.g. data model or controller) to data binding origin (e.g. widget).
16
17 format: function(value, constraints){
18 // summary:
19 // The converter function used when the data comes from data binding source (e.g. data model or controller) to data binding origin (e.g. widget).
20 // value: Anything
21 // The data.
22 // constraints: Object
23 // The options for data conversion, which is: mixin({}, dataBindingTarget.constraints, dataBindingOrigin.constraints).
24 },
25
26 parse: function(value, constraints){
27 // summary:
28 // The converter function used when the data comes from data binding origin (e.g. widget) to data binding source (e.g. data model or controller).
29 // value: Anything
30 // The data.
31 // constraints: Object
32 // The options for data conversion, which is: mixin({}, dataBindingTarget.constraints, dataBindingOrigin.constraints).
33 }
34 };
35
36 dojox.mvc.sync.options = {
37 // summary:
38 // Data binding options.
39
40 // bindDirection: Number
41 // The data binding bindDirection, choose from: dojox.mvc.Bind.from, dojox.mvc.Bind.to or dojox.mvc.Bind.both.
42 bindDirection: dojox/mvc.both,
43
44 // converter: dojox/mvc/sync.converter
45 // Class/object containing the converter functions used when the data goes between data binding source (e.g. data model or controller) to data binding origin (e.g. widget).
46 converter: null,
47
48 // equals: Function
49 // The function to check if there really was a change when source/target dojo/Stateful indicates so.
50 // Should take two arguments, and should return true when those two are considered equal.
51 equals: null
52 };
53 =====*/
54
55 has.add("mvc-bindings-log-api", (config["mvc"] || {}).debugBindings);
56
57 var sync;
58
59 var getLogContent = (has("mvc-bindings-log-api")) ? function(/*dojo/Stateful*/ source, /*String*/ sourceProp, /*dojo/Stateful*/ target, /*String*/ targetProp){
60 return [
61 [target.canConvertToLoggable || !target.declaredClass ? target : target.declaredClass, targetProp].join(":"),
62 [source.canConvertToLoggable || !source.declaredClass ? source : source.declaredClass, sourceProp].join(":")
63 ];
64 } : "";
65
66 function equals(/*Anything*/ dst, /*Anything*/ src){
67 // summary:
68 // Returns if the given two values are equal.
69
70 return dst === src
71 || typeof dst == "number" && isNaN(dst) && typeof src == "number" && isNaN(src)
72 || lang.isFunction((dst || {}).getTime) && lang.isFunction((src || {}).getTime) && dst.getTime() == src.getTime()
73 || (lang.isFunction((dst || {}).equals) ? dst.equals(src) : lang.isFunction((src || {}).equals) ? src.equals(dst) : false);
74 }
75
76 function copy(/*Function*/ convertFunc, /*Object?*/ constraints, /*Function*/ equals, /*dojo/Stateful*/ source, /*String*/ sourceProp, /*dojo/Stateful*/ target, /*String*/ targetProp, /*Anything*/ old, /*Anything*/ current, /*Object?*/ excludes){
77 // summary:
78 // Watch for change in property in dojo/Stateful object.
79 // description:
80 // Called when targetProp property in target is changed. (This is mainly used as a callback function of dojo/Stateful.watch())
81 // When older value and newer value are different, copies the newer value to sourceProp property in source.
82 // convertFunc: Function
83 // The data converter function.
84 // constraints: Object?
85 // The data converter options.
86 // equals: Function
87 // The function to check if there really was a change when source/target dojo/Stateful indicates so.
88 // Should take two arguments, and should return true when those two are considered equal.
89 // source: dojo/Stateful
90 // The dojo/Stateful of copy source.
91 // sourceProp: String
92 // The property of copy source, specified in data binding. May be wildcarded.
93 // target: dojo/Stateful
94 // The dojo/Stateful of copy target.
95 // targetProp: String
96 // The property of copy target, being changed. For wildcard-based data binding, this is used as the property to be copied.
97 // old: Anything
98 // The older property value.
99 // current: Anything
100 // The newer property value.
101 // excludes: Object?
102 // The list of properties that should be excluded from wildcarded data binding.
103
104 // Bail if there is no change in value,
105 // or property name is wildcarded and the property to be copied is not in source property list (and source property list is defined),
106 // or property name is wildcarded and the property to be copied is in explicit "excludes" list
107 if(equals(current, old)
108 || sourceProp == "*" && array.indexOf(source.get("properties") || [targetProp], targetProp) < 0
109 || sourceProp == "*" && targetProp in (excludes || {})){ return; }
110
111 var prop = sourceProp == "*" ? targetProp : sourceProp;
112 if(has("mvc-bindings-log-api")){
113 var logContent = getLogContent(source, prop, target, targetProp);
114 }
115
116 try{
117 current = convertFunc ? convertFunc(current, constraints) : current;
118 }catch(e){
119 if(has("mvc-bindings-log-api")){
120 console.log("Copy from" + logContent.join(" to ") + " was not done as an error is thrown in the converter.");
121 }
122 return;
123 }
124
125 if(has("mvc-bindings-log-api")){
126 console.log(logContent.reverse().join(" is being copied from: ") + " (Value: " + current + " from " + old + ")");
127 }
128
129 // Copy the new value to source
130 lang.isFunction(source.set) ? source.set(prop, current) : (source[prop] = current);
131 }
132
133 var directions = {
134 // from: Number
135 // Data binding goes from the source to the target
136 from: 1,
137
138 // to: Number
139 // Data binding goes from the target to the source
140 to: 2,
141
142 // both: Number
143 // Data binding goes in both directions (dojox/mvc/Bind.from | dojox/mvc/Bind.to)
144 both: 3
145 }, undef;
146
147 sync = function(/*dojo/Stateful*/ source, /*String*/ sourceProp, /*dojo/Stateful*/ target, /*String*/ targetProp, /*dojox/mvc/sync.options*/ options){
148 // summary:
149 // Synchronize two dojo/Stateful properties.
150 // description:
151 // Synchronize two dojo/Stateful properties.
152 // source: dojo/Stateful
153 // Source dojo/Stateful to be synchronized.
154 // sourceProp: String
155 // The property name in source to be synchronized.
156 // target: dojo/Stateful
157 // Target dojo/Stateful to be synchronized.
158 // targetProp: String
159 // The property name in target to be synchronized.
160 // options: dojox/mvc/sync.options
161 // Data binding options.
162 // returns:
163 // The handle of data binding synchronization.
164
165 var converter = (options || {}).converter, converterInstance, formatFunc, parseFunc;
166 if(converter){
167 converterInstance = {source: source, target: target};
168 formatFunc = converter.format && lang.hitch(converterInstance, converter.format);
169 parseFunc = converter.parse && lang.hitch(converterInstance, converter.parse);
170 }
171
172 var _watchHandles = [],
173 excludes = [],
174 list,
175 constraints = lang.mixin({}, source.constraints, target.constraints),
176 bindDirection = (options || {}).bindDirection || mvc.both,
177 equals = (options || {}).equals || sync.equals;
178
179 if(has("mvc-bindings-log-api")){
180 var logContent = getLogContent(source, sourceProp, target, targetProp);
181 }
182
183 if(targetProp == "*"){
184 if(sourceProp != "*"){ throw new Error("Unmatched wildcard is specified between source and target."); }
185 list = target.get("properties");
186 if(!list){
187 list = [];
188 for(var s in target){ if(target.hasOwnProperty(s) && s != "_watchCallbacks"){ list.push(s); } }
189 }
190 excludes = target.get("excludes");
191 }else{
192 list = [sourceProp];
193 }
194
195 if(bindDirection & mvc.from){
196 // Start synchronization from source to target (e.g. from model to widget). For wildcard mode (sourceProp == targetProp == "*"), the 1st argument of watch() is omitted
197 if(lang.isFunction(source.set) && lang.isFunction(source.watch)){
198 _watchHandles.push(source.watch.apply(source, ((sourceProp != "*") ? [sourceProp] : []).concat([function(name, old, current){
199 copy(formatFunc, constraints, equals, target, targetProp, source, name, old, current, excludes);
200 }])));
201 }else if(has("mvc-bindings-log-api")){
202 console.log(logContent.reverse().join(" is not a stateful property. Its change is not reflected to ") + ".");
203 }
204
205 // Initial copy from source to target (e.g. from model to widget)
206 array.forEach(list, function(prop){
207 // In "all properties synchronization" case, copy is not done for properties in "exclude" list
208 if(targetProp != "*" || !(prop in (excludes || {}))){
209 var value = lang.isFunction(source.get) ? source.get(prop) : source[prop];
210 copy(formatFunc, constraints, equals, target, targetProp == "*" ? prop : targetProp, source, prop, undef, value);
211 }
212 });
213 }
214
215 if(bindDirection & mvc.to){
216 if(!(bindDirection & mvc.from)){
217 // Initial copy from source to target (e.g. from model to widget)
218 array.forEach(list, function(prop){
219 // In "all properties synchronization" case, copy is not done for properties in "exclude" list
220 if(targetProp != "*" || !(prop in (excludes || {}))){
221 // Initial copy from target to source (e.g. from widget to model), only done for one-way binding from widget to model
222 var value = lang.isFunction(target.get) ? target.get(targetProp) : target[targetProp];
223 copy(parseFunc, constraints, equals, source, prop, target, targetProp == "*" ? prop : targetProp, undef, value);
224 }
225 });
226 }
227
228 // Start synchronization from target to source (e.g. from widget to model). For wildcard mode (sourceProp == targetProp == "*"), the 1st argument of watch() is omitted
229 if(lang.isFunction(target.set) && lang.isFunction(target.watch)){
230 _watchHandles.push(target.watch.apply(target, ((targetProp != "*") ? [targetProp] : []).concat([function(name, old, current){
231 copy(parseFunc, constraints, equals, source, sourceProp, target, name, old, current, excludes);
232 }])));
233 }else if(has("mvc-bindings-log-api")){
234 console.log(logContent.join(" is not a stateful property. Its change is not reflected to ") + ".");
235 }
236 }
237
238 if(has("mvc-bindings-log-api")){
239 console.log(logContent.join(" is bound to: "));
240 }
241
242 var handle = {};
243 handle.unwatch = handle.remove = function(){
244 for(var h = null; h = _watchHandles.pop();){
245 h.unwatch();
246 }
247 if(has("mvc-bindings-log-api")){
248 console.log(logContent.join(" is unbound from: "));
249 }
250 };
251 return handle; // dojo/handle
252 };
253
254 lang.mixin(mvc, directions);
255
256 // lang.setObject() thing is for back-compat, remove it in 2.0
257 return lang.setObject("dojox.mvc.sync", lang.mixin(sync, {equals: equals}, directions));
258 });