PDA

View Full Version : My little overrides to Store and BasicForm



ethraza
16 Dec 2010, 5:42 AM
I'm posting it here in the hope it will be useful for someone else.
Some ExtJs core developer can find it kind of inspirational too and add something similar to ExtJs 4, maybe? :)

The truth is that I'm using ExtJs 3.3.1 but can't develop any ExtJs system anymore without that little overrides I created myself. They are so useful to me!



Ext.form.BasicForm.override({
/**
* Get values from a form as a object
*
* @param {Object} (optional) dirtyOnly - If true returns only the dirty fields
* @param {Object} (optional) submitValueOnly - If true returns only the submitValue:true fields
* @param {Function} (optional) prepareValue - A function to prepare the field value before returns it.
* If passed it receives (field.getValue(), field) and needs to return a field value
* @return {Object} (optional) fields - Returns a object with of field names and field values
*/
getFieldValues : function(dirtyOnly, submitValueOnly, prepareValue){
var o = {},
n,
key,
val;
if (!Ext.isFunction(prepareValue))
prepareValue = function(v){return v;}

this.items.each(function(f) {
if ((submitValueOnly !== true || f.submitValue !== false) && dirtyOnly !== true || f.isDirty()) {
n = f.getName();
key = o[n];
val = prepareValue(f.getValue(), f);

if(Ext.isDefined(key)){
if(Ext.isArray(key)){
o[n].push(val);
}else{
o[n] = [key, val];
}
}else{
o[n] = val;
}
}
});
return o;
}
});

Ext.data.Store.override({
/**
* Get Modified Itens from a Store
*
* @param {Object} (optional) aFields - Array of fields you want to collect
* @param {Object} (optional) fValue - A function to process each record
* @return {Array} Array of modified items
*/
getModifieds : function(aFields, fValue){ //fValue(mValue, [sField]:When aFields = true)
var fValue = (fValue && typeof fValue == 'function')? fValue : function(v) {return v}
var aMod = [];

Ext.each(this.getModifiedRecords(),function(e){
if (aFields) {
var obj = {};

Ext.each(aFields, function(f){
obj[f] = fValue(e.get(f), f);
});

aMod.push(obj);
} else {
aMod.push(fValue(e.data));
}
});

if (aMod.length) {
this.modified = [];
return Ext.encode(aMod);
} else {
return ''
}
},

/**
* Add a cloned record to a Store
*
* @param {Object} rec - The record to clone and add
* @param {Object} (optional) oPrn - Object of values to set in the record
*/
addRecordCopy : function(rec, oPrn){
if (rec) {
var n = rec.copy();
Ext.data.Record.id(n);

if (oPrn) {
Ext.iterate(oPrn, function(k,v){
n.set(k,v);
});
}

this.add(n);
}
}
});
Usage Examples:


var submitForm2Store = function(){
var f = component.mainForm.form;

if (f.isValid()) {
var v = f.getFieldValues(false, true, function(v,f){
return (Ext.isDate(v))? v.format('Y-m-d') : v;
});

if (v) {
docDs.load({
params: v
});
}
}
}


var g = component.mainGrid.store.getModifieds(['id']);
if (g)
Ext.apply(a.options.params, {ids: g});


function(){
var g = component.mainCombo;
g = g.store.getAt(g.selectedIndex);

if (g && (component.mainGrid.store.find('id', g.data.id) < 0))
component.mainGrid.store.addRecordCopy(g);
}