| 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 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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( source, sourceProp, target, 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( dst, src){ |
| 67 |
|
| 68 |
|
| 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( convertFunc, constraints, equals, source, sourceProp, target, targetProp, old, current, excludes){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 130 |
lang.isFunction(source.set) ? source.set(prop, current) : (source[prop] = current); |
| 131 |
} |
| 132 |
|
| 133 |
var directions = { |
| 134 |
|
| 135 |
|
| 136 |
from: 1, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
to: 2, |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
both: 3 |
| 145 |
}, undef; |
| 146 |
|
| 147 |
sync = function( source, sourceProp, target, targetProp, options){ |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 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 |
|
| 206 |
array.forEach(list, function(prop){ |
| 207 |
|
| 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 |
|
| 218 |
array.forEach(list, function(prop){ |
| 219 |
|
| 220 |
if(targetProp != "*" || !(prop in (excludes || {}))){ |
| 221 |
|
| 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 |
|
| 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; |
| 252 |
}; |
| 253 |
|
| 254 |
lang.mixin(mvc, directions); |
| 255 |
|
| 256 |
|
| 257 |
return lang.setObject("dojox.mvc.sync", lang.mixin(sync, {equals: equals}, directions)); |
| 258 |
}); |