partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Ext.form.Basic-AssociationSupport.js (1986B)


      1 /**
      2  * Overrides Ext.form.Basic to implement getter support for loadRecord(). This enables us to directly
      3  * assign comboboxes to associations.
      4  */
      5 Ext.define("PartKeepr.form.Basic", {
      6     override: "Ext.form.Basic",
      7 
      8     loadRecord: function (record)
      9     {
     10         this._record = record;
     11 
     12         var values = record.getData();
     13 
     14         for (var i in record.associations) {
     15             var getterName = record.associations[i].getterName;
     16             values[i] = record[getterName]();
     17         }
     18 
     19         return this.setValues(values);
     20     },
     21     /**
     22      * Persists the values in this form into the passed {@link Ext.data.Model} object in a beginEdit/endEdit block.
     23      * If the record is not specified, it will attempt to update (if it exists) the record provided to loadRecord.
     24      * @param {Ext.data.Model} [record] The record to edit
     25      * @return {Ext.form.Basic} this
     26      */
     27     updateRecord: function(record) {
     28         record = record || this._record;
     29         if (!record) {
     30             //<debug>
     31             Ext.raise("A record is required.");
     32             //</debug>
     33             return this;
     34         }
     35 
     36         var fields = record.self.fields,
     37             values = this.getFieldValues(),
     38             obj = {},
     39             associations = {},
     40             i = 0,
     41             len = fields.length,
     42             name;
     43 
     44         for (; i < len; ++i) {
     45             name  = fields[i].name;
     46 
     47             if (values.hasOwnProperty(name)) {
     48                 if (record.hasField(name)) {
     49                     obj[name] = values[name];
     50                 } else {
     51                     associations[name] = values[name];
     52                 }
     53             }
     54         }
     55 
     56         record.beginEdit();
     57         record.set(obj);
     58 
     59         for (i in associations) {
     60             if (record.associations[i]) {
     61                 var setterName = record.associations[i].setterName;
     62                 record[setterName](associations[i]);
     63             }
     64         }
     65         record.endEdit();
     66 
     67         return this;
     68     },
     69 });