123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- define("dojox/mvc/_DataBindingMixin", [
- "dojo/_base/lang",
- "dojo/_base/array",
- "dojo/_base/declare",
- "dojo/Stateful",
- "dijit/registry"
- ], function(lang, array, declare, Stateful, registry){
-
- return declare("dojox.mvc._DataBindingMixin", null, {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ref: null,
-
-
- isValid: function(){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return this.get("binding") ? this.get("binding").get("valid") : true;
- },
-
- _dbstartup: function(){
-
-
-
-
-
- if(this._databound){
- return;
- }
- this._unwatchArray(this._viewWatchHandles);
-
- this._viewWatchHandles = [
-
- this.watch("ref", function(name, old, current){
- if(this._databound){
- this._setupBinding();
- }
- }),
-
- this.watch("value", function(name, old, current){
- if(this._databound){
- var binding = this.get("binding");
- if(binding){
-
- if(!((current && old) && (old.valueOf() === current.valueOf()))){
- binding.set("value", current);
- }
- }
- }
- })
- ];
- this._beingBound = true;
- this._setupBinding();
- delete this._beingBound;
- this._databound = true;
- },
-
- _setupBinding: function(parentBinding){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(!this.ref){
- return;
- }
- var ref = this.ref, pw, pb, binding;
-
- if(ref && lang.isFunction(ref.toPlainObject)){
- binding = ref;
- }else if(/^\s*expr\s*:\s*/.test(ref)){
- ref = ref.replace(/^\s*expr\s*:\s*/, "");
- binding = lang.getObject(ref);
- }else if(/^\s*rel\s*:\s*/.test(ref)){
- ref = ref.replace(/^\s*rel\s*:\s*/, "");
- parentBinding = parentBinding || this._getParentBindingFromDOM();
- if(parentBinding){
- binding = lang.getObject("" + ref, false, parentBinding);
- }
- }else if(/^\s*widget\s*:\s*/.test(ref)){
- ref = ref.replace(/^\s*widget\s*:\s*/, "");
- var tokens = ref.split(".");
- if(tokens.length == 1){
- binding = registry.byId(ref).get("binding");
- }else{
- pb = registry.byId(tokens.shift()).get("binding");
- binding = lang.getObject(tokens.join("."), false, pb);
- }
- }else{
- parentBinding = parentBinding || this._getParentBindingFromDOM();
- if(parentBinding){
- binding = lang.getObject("" + ref, false, parentBinding);
- }else{
- try{
- if(lang.getObject(ref) instanceof Stateful){
- binding = lang.getObject(ref);
- }
- }catch(err){
- if(ref.indexOf("${") == -1){
- throw new Error("dojox.mvc._DataBindingMixin: '" + this.domNode +
- "' widget with illegal ref expression: '" + ref + "'");
- }
- }
- }
- }
- if(binding){
- if(lang.isFunction(binding.toPlainObject)){
- this.binding = binding;
- this._updateBinding("binding", null, binding);
- }else{
- throw new Error("dojox.mvc._DataBindingMixin: '" + this.domNode +
- "' widget with illegal ref not evaluating to a dojo.Stateful node: '" + ref + "'");
- }
- }
- },
- _isEqual: function(one, other){
-
- return one === other ||
-
- isNaN(one) && typeof one === 'number' &&
- isNaN(other) && typeof other === 'number';
- },
- _updateBinding: function(name, old, current){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this._unwatchArray(this._modelWatchHandles);
-
- var binding = this.get("binding");
- if(binding && lang.isFunction(binding.watch)){
- var pThis = this;
- this._modelWatchHandles = [
-
- binding.watch("value", function (name, old, current){
- if(pThis._isEqual(old, current)){return;}
- if(pThis._isEqual(pThis.get('value'), current)){return;}
- pThis.set("value", current);
- }),
-
- binding.watch("valid", function (name, old, current){
- pThis._updateProperty(name, old, current, true);
- if(current !== pThis.get(name)){
- if(pThis.validate && lang.isFunction(pThis.validate)){
- pThis.validate();
- }
- }
- }),
-
- binding.watch("required", function (name, old, current){
- pThis._updateProperty(name, old, current, false, name, current);
- }),
-
- binding.watch("readOnly", function (name, old, current){
- pThis._updateProperty(name, old, current, false, name, current);
- }),
-
- binding.watch("relevant", function (name, old, current){
- pThis._updateProperty(name, old, current, false, "disabled", !current);
- })
- ];
- var val = binding.get("value");
- if(val != null){
- this.set("value", val);
- }
- }
- this._updateChildBindings();
- },
-
- _updateProperty: function(name, old, current, defaultValue, setPropName, setPropValue){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(old === current){
- return;
- }
- if(current === null && defaultValue !== undefined){
- current = defaultValue;
- }
- if(current !== this.get("binding").get(name)){
- this.get("binding").set(name, current);
- }
- if(setPropName){
- this.set(setPropName, setPropValue);
- }
- },
- _updateChildBindings: function(parentBind){
-
-
-
-
-
-
-
-
-
-
-
- var binding = this.get("binding") || parentBind;
- if(binding && !this._beingBound){
- array.forEach(registry.findWidgets(this.domNode), function(widget){
- if(widget.ref && widget._setupBinding){
- widget._setupBinding(binding);
- }else{
- widget._updateChildBindings(binding);
- }
- });
- }
- },
- _getParentBindingFromDOM: function(){
-
-
-
-
-
-
-
- var pn = this.domNode.parentNode, pw, pb;
- while(pn){
- pw = registry.getEnclosingWidget(pn);
- if(pw){
- pb = pw.get("binding");
- if(pb && lang.isFunction(pb.toPlainObject)){
- break;
- }
- }
- pn = pw ? pw.domNode.parentNode : null;
- }
- return pb;
- },
- _unwatchArray: function(watchHandles){
-
-
-
-
-
-
- array.forEach(watchHandles, function(h){ h.unwatch(); });
- }
- });
- });
|