PDA

View Full Version : access a model property from a field convert function



macs
16 May 2012, 11:44 PM
i add a property to a model with default value 50 before it is instantiated like shown below.


var propertyValue = 50;
this.myStore.getModel().addStatics({
modelProperty: propertyValue,
});
this.myStore.load();

i then want to access this property from a field convert function like below, however, this returns 'undefined' in the log. also when i hard code the property in the model, i am not able to access its value. Is there a way to do this? many thanks in advance for any suggestions

//i omitted the rest of the snippet for the ease of reading


fields: [
{name: 'id'},
{name: 'distance',
convert: function (value, record) {
console.log(this.modelProperty)
},
},
],

sword-it
17 May 2012, 1:17 AM
Try this:-



var propertyValue = 50;
this.myStore.getModel().addStatics({
modelProperty: propertyValue,
});
this.myStore.load();

instead of
var propertyValue = 50;


try
this.propertyValue = 50;

also remove ',' after propertyValue in addStatistics

macs
17 May 2012, 5:08 AM
thanks sword-it for the suggestion. however the question was not so much about how can i put the property in the model, but rather how can i access it from the fields convert function? as i said also when i put the property hard coded into the model i am not able to access it form the fields convert function.

coolfish
11 Sep 2012, 6:09 AM
The static property or function will be added to the Model (class) is my guess, but not to instantiated objects of that model. So in your convert function you'd use it along the lines of



function convert(v, record)
{
var adjustedValue = MyApp.model.MyClass.myStaticProperty + record.foo ;
return adjustedValue;
}