PDA

View Full Version : extend class with custom method help



edmeehan
30 Sep 2010, 12:43 PM
So I am trying to learn extjs... and can get my way around pretty will with the current objects and methods... but I now want to start playing with extending the classes. So I have a pretty simple function I have been using.. and decided I should try and use it as a method of the data.store object. Seems simple enough... but no dice.


Ext.extend(Ext.data.Store,{
getOtherValue: function(property,value,returnProperty){
var fn = this.find(property,value);
return this.data.items[fn].data[returnProperty]
}
});


So I keep getting a "getOtherValue is not a function" when I use it like so


myStore.getOtherValue('label',value,'label');


Any help here would be great... I am new to OOP and want to improve.

edmeehan
30 Sep 2010, 3:36 PM
// Apply custom namespace
Ext.namespace('BLAH','BLAH.data');
// My Custome Class Extensions
BLAH.data.SimpleStore = Ext.extend(Ext.data.SimpleStore,{
getOtherValue: function(property,value,returnProperty){
var fn = this.find(property,value);
return this.data.items[fn].data[returnProperty]
}
});

myStore.getOtherValue('value',0,'label')


This works... but was hoping I could extend Ext.data.Store and have all my stores inherit this method since they are children. Is my thinking off? Is this not the whole idea behind the OOP. Seems silly if I need to write an extension for each store type.

Can't I just extend the Ext.data.Store and then write new Ext.data.JsonStore and have it use my new extended method? Any one of an idea for this... or am I looking at the wrong parent object?

laurentParis
30 Sep 2010, 9:27 PM
Ext.override(Ext.data.Store,{
getOtherValue: function(property,value,returnProperty){
var fn = this.find(property,value);
return this.data.items[fn].data[returnProperty]
}
});

edmeehan
30 Sep 2010, 10:06 PM
Awesome!! works great.


Ext.override(Ext.data.Store,{
getOtherValue: function(property,value,returnProperty){
//set to return one value, but maybe have it return an array of values in the future
//this.clearFilter();
var fn = this.find(property,value);
return fn!=-1 ? this.data.items[fn].data[returnProperty] : null;
}
});

Here it is with the condition added... works great... now I can't wait to explore and extend more classes.