PDA

View Full Version : Getting data out of a store and using it for further processing



BukhtiarM
6 Jun 2014, 8:12 AM
Dear All, I am a newbie and have been learning EXT-js 4 for a month already. I am new to most web application technologies like Ajax for instance. I am creating my first Ext app and am trying to use MVC architecture. I have a user input form that inputs certain data it is any concentration for a chemical. I have certain data stored on the server in a json file i want to load that data into a store and compare if the values entered by the user fall into any of the ranges defined in that data. let me give you some code

limits.json


{
"limits":[
{
"level": 3,
"value": 1.5,
"name":"ERPG3"
},{
"level": 2,
"value": 0.15,
"name":"ERPG2"
},{
"level": 1,
"value": 0.05,
"name":"ERPG1"
}
]
}

Model (chem.Limits)


Ext.define('PLUSES.model.chem.Limits', {
extend: 'Ext.data.Model',
fields: [
{name: 'level', type: 'int'},
{name: 'value', type: 'int'},
{name: 'name', type: 'string'}
]
});

controller

// The controller is too lengthy and so i am putting only the relevant code here
// I create an instance of the model and load the data from the json file into the store when i display it in
//grid it works fine but i want to use this data and compare it to certain other values... how to do that I
//have tried many different suggestion in different forums the whole day and now i am fed up and
//thought i should ask this again (i know it has been asked many times and answered many times).


var limitstore = Ext.create('Ext.data.Store', {
model: 'PLUSES.model.chem.Limits',
autoLoad: true,
proxy: {
type: 'ajax',
url : 'resources/AdviceCenter/Acrolein/limits.json',
reader: {
type: 'json',
root: 'limits'
}
}
});
console.log(limitstore);

// The above line gives me some data in the console, I can not find the actual data anywhere.
// The following line gives me 0
console.log(limitstore.getTotalCount());


But it does display data in grid panel for sure :s
Any help would be appreciated. Okay I was playing around with the Ext.data.Store methods and realized that if i add the following lines I can some how access the store values


limitstore.load({
scope: this,
callback: function(records, operation, success) {
// the operation object
// contains all of the details of the load operation
console.log(limitstore.getTotalCount());
}
});

This returns 3 in the console... Do I need to write all the logic in the callback function (does not sound right to me =;)... because if i write the console.log(store.getTotalCount()); line "after" the store.load it gives me 0 again

Scott Mildenberger
6 Jun 2014, 11:35 AM
On whatever event you want this to happen you can get an instance of the store and then use queryBy() to find matching records like


var values = dsTrans.queryBy(function(record) {
return record.get('GROUPER') === Invoice;
});

for (var i = 0, len = values.length; i < len; i++) {
var description = values.getAt(i).get('KDIDES'),





dsTrans above is my store. You write the function to determine if a record matches the criteria. It returns an array of matching records.

BukhtiarM
24 Jun 2014, 7:38 AM
Thanks a lot i faced a lot of problems but this method definitely works guys