| 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( target, 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( w){ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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; |
| 38 |
} |
| 39 |
} |
| 40 |
pn = pw && pw.domNode.parentNode; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function bind( source, sourceProp, target, targetProp, options){ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 86 |
_handles["Two"] = sync(resolvedSource, sourceProp, resolvedTarget, targetProp, options); |
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
dataBindAttr: "data-mvc-bindings", |
| 116 |
|
| 117 |
_dbpostscript: function( params, srcNodeRef){ |
| 118 |
|
| 119 |
|
| 120 |
|
| 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( bindWith){ |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
this.canConvertToLoggable = true; |
| 151 |
|
| 152 |
var refs = this._refs; |
| 153 |
if(refs){ |
| 154 |
var atWatchHandles = this._atWatchHandles = this._atWatchHandles || {}; |
| 155 |
|
| 156 |
|
| 157 |
this._excludes = null; |
| 158 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
for(var s in this._atWatchHandles){ |
| 177 |
this._atWatchHandles[s].unwatch(); |
| 178 |
delete this._atWatchHandles[s]; |
| 179 |
} |
| 180 |
}, |
| 181 |
|
| 182 |
_setAtWatchHandle: function( name, value){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 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 |
|
| 193 |
var atWatchHandles = this._atWatchHandles = this._atWatchHandles || {}; |
| 194 |
if(atWatchHandles[name]){ |
| 195 |
atWatchHandles[name].unwatch(); |
| 196 |
delete atWatchHandles[name]; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
this[name] = null; |
| 201 |
|
| 202 |
|
| 203 |
this._excludes = null; |
| 204 |
|
| 205 |
if(this._started){ |
| 206 |
|
| 207 |
atWatchHandles[name] = bind(value.target, value.targetProp, this, name, {bindDirection: value.bindDirection, converter: value.converter, equals: value.equalsCallback}); |
| 208 |
}else{ |
| 209 |
|
| 210 |
this._refs[name] = value; |
| 211 |
} |
| 212 |
}, |
| 213 |
|
| 214 |
_setBind: function( value){ |
| 215 |
|
| 216 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
if(this._excludes){ |
| 234 |
return this._excludes; |
| 235 |
} |
| 236 |
var list = []; |
| 237 |
for(var s in this._atWatchHandles){ |
| 238 |
if(s != "*"){ list.push(s); } |
| 239 |
} |
| 240 |
return list; |
| 241 |
}, |
| 242 |
|
| 243 |
_getPropertiesAttr: function(){ |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if(this.constructor._attribs){ |
| 250 |
return this.constructor._attribs; |
| 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; |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
mixin[mixin.dataBindAttr] = ""; |
| 262 |
|
| 263 |
var _atBindingMixin = declare("dojox/mvc/_atBindingMixin", null, mixin); |
| 264 |
_atBindingMixin.mixin = mixin; |
| 265 |
return _atBindingMixin; |
| 266 |
}); |