PDA

View Full Version : how to retrieve data from a store json and save it in a gobal variable ..



fmuke
19 Oct 2012, 7:54 AM
how to retrieve data from a store json and save it in a gobal variable ..
i am getting data from a store ... i am able to loop through the json record..
but i have been unable to save what ever data in a gobal variable in order to use it for
another function



var
store2 = Ext.create('Ext.data.ArrayStore', {
id: 'graphStore2',
autoLoad: true,
proxy: new Ext.data.HttpProxy({
type: 'ajax',
method: 'GET',

//url: 'data/resources/jsons/graph.json',
//url: 'data/resources/jsons/graphNested.json',
//url: '../Services/NukeappsSiteWebService.asmx/GetJsonStructure',
url: 'data/resources/jsons/graphMultiple2.json',
reader: {
type: 'json',
root: 'data'
}
}),
fields: modelVar3,
NameStringStore: "name",
IdIntStore: 1,
listeners: {?
beforeload: function(store, operation, options) {},
load: function(store, records) {

for (var i = 0; i < records.length; i++) {

var me = this;

var visits = records[i].get('Apple');
visitsRecord = records.length;
visit = 8;
//<--- global variable
//console.log(' IdIntStore ---> ' + IdIntStore);
if (i == records.length - 1) console.log(visitsRecord);

return 8;
}
}
}
});?

vietits
20 Oct 2012, 4:52 PM
What is the global variable you want to save data to? Where is it declared? What do you want to save to it?

metalinspired
21 Oct 2012, 6:09 AM
Why would you save a store in global variable when you can use Ext.getStore() (http://docs.sencha.com/ext-js/4-1/#!/api/Ext-method-getStore) anywhere in your code to fetch created store.

fmuke
22 Oct 2012, 7:06 AM
thanks for your replies... the reason why i need a globar variable is that i need to dynamicaly create a model, and a chart in order to display a chart line with multiple line dynamically ... x axis month, y axis data int ... also i need to display company name for each line ... since i m receiving data from the sql server... in json format... i will need to loop through data a save infos accordingly in var variable to dynamically create model and chart..

metalinspired
22 Oct 2012, 9:30 AM
Take a look here (http://snook.ca/archives/javascript/global_variable).
Global variables are a last resort and are mostly avoided, but hey, if it solves your problem go for it.

fmuke
23 Oct 2012, 5:00 AM
thank you very much for your replies.
i'm new at ext js but i am not new at programming.
i'm not having a problem with global variables but with how ext js deals with data coming from a web service.
I need to be able to get the data from a store an used it either to modify the current store or to create a new one. this is the reason why im using global variables.

for that i need to load the store because data from a store can only be return or checked when i has been run. so i used a callback method along with store.load .
with more infos : loading is asynchronous. this call will return before the new data has been loaded . to perform any post processing where information from the load call is required, specify the callback function to be called , or use a load event handler.

but my code below do not work :
//---> visitload which is Global variable maintain the value 21 instead of 23 after store2 loads
var store2 = Ext.create('Ext.data.ArrayStore', {
id: 'graphStore2',
autoLoad: true,
callback: function (records, operation, success, r) {
var test;
visitload = 21;// works fine


var fieldValue;

store2.each(function (record) {
record.fields.each(function (field) {
fieldValue = record.get(field.name);
});
visitload = 23; //<---- i need this value
// why do i want visiload to have value 23. well that is because i will need to save in this
// variable values will be necesserity to create a new store
}, this);

//visitload = 24; //work fines
var jsonArray = store2.data.items
var jsonData = Ext.encode(Ext.pluck(store2.data.items, 'data'));

return test;
},
proxy: new Ext.data.HttpProxy({
type: 'ajax',
method: 'GET',
//url: 'data/resources/jsons/graph.json',
//url: 'data/resources/jsons/graphNested.json',
//url: '../Services/NukeappsSiteWebService.asmx/GetJsonStructure',
url: 'data/resources/jsons/graphMultiple2.json',
reader: {
type: 'json',
records: 'jsonx',
root: 'data'
}
}),
fields: modelVar3,

NameStringStore: "name",

IdIntStore: 1,
listeners: {


beforeload: function (store, operation, options) {
},

load: function (store, records, success) {
store2.on('load', function (store, records, options) {
var fieldValue;

store2.each(function (record) {
record.fields.each(function (field) {
fieldValue = record.get(field.name);
});
visitload = 23;
}, this);
});
}
},

getData: function () {

return 8;
}
});

console.log (visiload); // the value remains 21

metalinspired
23 Oct 2012, 9:39 AM
Please encapsulate your code snippet into


Its too hard to read this way