Changeset 233

Show
Ignore:
Timestamp:
05/10/07 20:34:29 (6 years ago)
Author:
stefanp
Message:

introduced getter and setter field for instances of jala.Form. these functions are used if no getter/setter is provided for a component. By default they use the static jala.Form.propertyGetter/Setter which return fields of the data object.

fixed date component: default value is used only if it really is a date, all other data types are omitted.

removed Component.File#setValue again. It's ok to force users to add a setter method (actually thought this method would fix a bug, but that turned out not to be a bug at all).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • jala/trunk/code/Form.js

    r232 r233  
    9090 
    9191 
     92   /** 
     93    * The default getter function for this form. Unless a getter 
     94    * is specified for the component, this function is called 
     95    * to retrieve the original value of a field. 
     96    * When called, the scope is set to the data object and 
     97    * the name of the element is the sole argument. 
     98    * @see jala.Form.Component.Input#getValue 
     99    * @type Function 
     100    */ 
     101   this.getter;      // for doc purposes only 
     102 
     103   // that's where the value really is stored: 
     104   var getter = jala.Form.propertyGetter; 
     105 
     106   this.__defineGetter__("getter", function() {   return getter;   }); 
     107   this.__defineSetter__("getter", function(newGetter) { 
     108      if (newGetter instanceof Function) { 
     109         getter = newGetter; 
     110      } 
     111   }); 
     112 
     113 
     114   /** 
     115    * The default setter function for this form. Unless a getter 
     116    * is specified for the component, this function is called to 
     117    * store the a value of a field. 
     118    * When called, the scope is set to the data object and 
     119    * the name and value of the element are provided as arguments. 
     120    * @see jala.Form.Component.Input#setValue 
     121    * @type Function 
     122    */ 
     123   this.setter;      // for doc purposes only 
     124 
     125   // that's where the value really is stored: 
     126   var setter = jala.Form.propertySetter; 
     127 
     128   this.__defineGetter__("setter", function() {   return setter;   }); 
     129   this.__defineSetter__("setter", function(newSetter) { 
     130      if (newSetter instanceof Function) { 
     131         setter = newSetter; 
     132      } 
     133   }); 
     134 
    92135   var tracker = undefined; 
    93136    
     
    275318   if (config.errorMessage) { 
    276319      form.setErrorMessage(config.errorMessage); 
     320   } 
     321   if (config.getter) { 
     322      form.getter = config.getter; 
     323   } 
     324   if (config.setter) { 
     325      form.setter = config.setter; 
    277326   } 
    278327   if (config.components) { 
     
    10521101    * function is called to retrieve the original value of the 
    10531102    * field. When called, the scope is set to the data object and 
    1054     * the name of the element as sole argument. 
     1103    * the name of the element is the sole argument. 
    10551104    * @see #getValue 
    10561105    * @type Function 
     
    11751224      // handling re-rendering 
    11761225      return null; 
    1177    } else if (this.getter) { 
    1178       return this.getter.call(this.form.getDataObject(), this.name); 
    11791226   } else { 
    1180       return this.form.getDataObject()[this.name]; 
     1227      var getter = (this.getter) ? this.getter : this.form.getter; 
     1228      return getter.call(this.form.getDataObject(), this.name); 
    11811229   } 
    11821230}; 
     
    11971245 */ 
    11981246jala.Form.Component.Input.prototype.setValue = function(destObj, value) { 
    1199    // default value for the setter is undefined, so if it has been 
     1247   // default value for this.setter is undefined, so if it has been 
    12001248   // set to explicitly null, we don't save the value. in this case, 
    12011249   // we assume, the property is handled outside of jala.Form or purposely 
    12021250   // ignored at all. 
    12031251   if (this.setter !== null) { 
    1204       if (this.setter) { 
    1205          this.setter.call(destObj, this.name, value); 
    1206       } else { 
    1207          destObj[this.name] = value; 
    1208       } 
     1252      var setter = (this.setter) ? this.setter : this.form.setter; 
     1253      setter.call(destObj, this.name, value); 
    12091254   } 
    12101255   return; 
     
    16571702   if (reqData) { 
    16581703      attr.value = reqData[this.name]; 
    1659    } else  if (value) { 
     1704   } else  if (value instanceof Date) { 
    16601705      attr.value = this.getDateFormat().format(value); 
    16611706   } 
     
    20432088}; 
    20442089 
    2045 /** 
    2046  * Overrides Component.Input#setValue. For file uploads, there is no 
    2047  * default property saving. A file upload can either be handled using 
    2048  * a setter method or saved from outside the save method. 
    2049  * @param {Object} destObj (optional) object whose values will be changed. 
    2050  * @param {Object} value The value to set the property to 
    2051  * @returns True in case the update was successful, false otherwise. 
    2052  * @see jala.Form#setter 
    2053  */ 
    2054 jala.Form.Component.File.prototype.setValue = function(destObj, value) { 
    2055    if (this.setter) { 
    2056       this.setter.call(destObj, this.name, value); 
    2057    } 
    2058    return; 
    2059 }; 
    2060  
    2061  
    2062  
    2063  
    2064  
    20652090 
    20662091/** 
     
    21292154 
    21302155 
    2131  
    2132  
     2156/** 
     2157 * static default getter function used to return a field  
     2158 * from the data object. 
     2159 * @param {String} name Name of the property. 
     2160 * @type Object 
     2161 */ 
     2162jala.Form.propertyGetter = function(name, value) { 
     2163   return this[name]; 
     2164}; 
     2165 
     2166/** 
     2167 * static default setter function used to change a field  
     2168 * of the data object. 
     2169 * @param {String} name Name of the property. 
     2170 * @param {Object} value New value of the property. 
     2171 */ 
     2172jala.Form.propertySetter = function(name, value) { 
     2173   this[name] = value; 
     2174}; 
    21332175 
    21342176