| 1 | define([ |
| 2 | "dojo/_base/declare", |
| 3 | "dojo/_base/lang", |
| 4 | "dojo/Stateful", |
| 5 | "./_atBindingMixin" |
| 6 | ], function(declare, lang, Stateful, _atBindingMixin){ |
| 7 | return declare("dojox.mvc._Controller", [Stateful, _atBindingMixin], { |
| 8 | postscript: function(/*Object?*/ params, /*DomNode|String?*/ srcNodeRef){ |
| 9 | // summary: |
| 10 | // If this object is not called from Dojo parser, starts this up right away. |
| 11 | // Also, if widget registry is available, register this object. |
| 12 | |
| 13 | // If there is dijit/_WidgetBase in upper class hierarchy (happens when this descendant is mixed into a widget), let _WidgetBase do all work |
| 14 | if(this._applyAttributes){ |
| 15 | return this.inherited(arguments); |
| 16 | } |
| 17 | // Look for dojox/mvc/at handles in the parameters |
| 18 | this._dbpostscript(params, srcNodeRef); |
| 19 | // Merge the parameters to this |
| 20 | if(params){ |
| 21 | this.params = params; |
| 22 | for(var s in params){ |
| 23 | this.set(s, params[s]); |
| 24 | } |
| 25 | } |
| 26 | // Add this instance to dijit/registry, if it's available |
| 27 | var registry; |
| 28 | try{ |
| 29 | // Usage of dijit/registry module is optional. Do not use it if it's not already loaded. |
| 30 | registry = require("dijit/registry"); |
| 31 | this.id = this.id || (srcNodeRef || {}).id || registry.getUniqueId(this.declaredClass.replace(/\./g, "_")); |
| 32 | registry.add(this); |
| 33 | }catch(e){} |
| 34 | if(!srcNodeRef){ |
| 35 | // If this instance is not created via Dojo parser, start this up right away |
| 36 | this.startup(); |
| 37 | }else{ |
| 38 | // If this is created via Dojo parser, set widgetId attribute so that destroyDescendants() of parent widget works |
| 39 | srcNodeRef.setAttribute("widgetId", this.id); |
| 40 | } |
| 41 | }, |
| 42 | |
| 43 | startup: function(){ |
| 44 | // summary: |
| 45 | // Starts up data binding as this object starts up. |
| 46 | |
| 47 | if(!this._applyAttributes){ |
| 48 | this._startAtWatchHandles(); |
| 49 | } |
| 50 | // If there is dijit/_WidgetBase in upper class hierarchy (happens when this descendant is mixed into a widget), let _WidgetBase do all work |
| 51 | this.inherited(arguments); |
| 52 | }, |
| 53 | |
| 54 | destroy: function(){ |
| 55 | // summary: |
| 56 | // Stops data binding as this object is destroyed. |
| 57 | |
| 58 | this._beingDestroyed = true; |
| 59 | if(!this._applyAttributes){ |
| 60 | this._stopAtWatchHandles(); |
| 61 | } |
| 62 | // If there is dijit/_WidgetBase in upper class hierarchy (happens when this descendant is mixed into a widget), let _WidgetBase do all work |
| 63 | this.inherited(arguments); |
| 64 | if(!this._applyAttributes){ |
| 65 | try{ |
| 66 | // Remove this instance from dijit/registry |
| 67 | // Usage of dijit/registry module is optional. Do not use it if it's not already loaded. |
| 68 | require("dijit/registry").remove(this.id); |
| 69 | }catch(e){} |
| 70 | } |
| 71 | this._destroyed = true; |
| 72 | }, |
| 73 | |
| 74 | set: function(/*String*/ name, /*Anything*/ value){ |
| 75 | // summary: |
| 76 | // If the value given is dojox/mvc/at handle, use it for data binding. |
| 77 | // Otherwise, if setter function is there, use it. |
| 78 | // Otherwise, set the value to the data model or to this object. |
| 79 | // name: String |
| 80 | // The property name. |
| 81 | // value: Anything |
| 82 | // The property value. |
| 83 | |
| 84 | // If an object is used, iterate through object |
| 85 | if(typeof name === "object"){ |
| 86 | for(var x in name){ |
| 87 | if(name.hasOwnProperty(x)){ |
| 88 | this.set(x, name[x]); |
| 89 | } |
| 90 | } |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | if(!this._applyAttributes){ |
| 95 | if((value || {}).atsignature == "dojox.mvc.at"){ |
| 96 | // If dojox/mvc/at handle is given, use it for data binding |
| 97 | return this._setAtWatchHandle(name, value); |
| 98 | }else{ |
| 99 | // Otherwise align the setter interface to _WidgetBase |
| 100 | var setterName = "_set" + name.replace(/^[a-z]/, function(c){ return c.toUpperCase(); }) + "Attr"; |
| 101 | if(this[setterName]){ |
| 102 | this[setterName](value); |
| 103 | }else{ |
| 104 | this._set(name, value); |
| 105 | } |
| 106 | return this; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // If there is dijit/_WidgetBase in upper class hierarchy (happens when this descendant is mixed into a widget), let _WidgetBase do all work |
| 111 | return this.inherited(arguments); |
| 112 | }, |
| 113 | |
| 114 | _set: function(/*String*/ name, /*Anything*/ value){ |
| 115 | // summary: |
| 116 | // Implement _set() interface so that _set() behavior is consistent whether the instance inherits _WidgetBase or not. |
| 117 | // If the instance does not inherit _WidgetBase, use dojo/Stateful/_changeAttrValue() that's equivalent to dijit/_WidgetBase._set(). |
| 118 | // name: String |
| 119 | // The property name. |
| 120 | // value: Anything |
| 121 | // The property value. |
| 122 | |
| 123 | if(!this._applyAttributes){ |
| 124 | // Call dojo/Stateful/_changeAttrValue() that's equivalent to dijit/_WidgetBase/_set() |
| 125 | return this._changeAttrValue(name, value); |
| 126 | } |
| 127 | // If there is dijit/_WidgetBase in upper class hierarchy (happens when this descendant is mixed into a widget), let _WidgetBase do all work |
| 128 | return this.inherited(arguments); |
| 129 | } |
| 130 | }); |
| 131 | }); |