PDA

View Full Version : Is it possible to create a store without passing "fields" or "model"?



kislay.kishore
11 Jan 2012, 8:21 AM
Is it possible to create a store without passing "fields" or "model"? I am actually trying to create a dynamic chart. So, series will vary , it may be someimes multiple, say n. So, for creating a store for n numuber of fields, i need to pass a string array in fields, of a store.

As I know the number of fields only when I parse json array to get the keys,
Was wondering if there is any way to create a store just by passing json array?

Even if I use a model, I would need to pass the fields.So, is there really a way to avoid this?

börn
11 Jan 2012, 8:55 AM
So I think you want to build a store dynamically for some kind of metadata sent by the server. There are some threads around discussing that problem I think? I did this today too.. just by defining my model inline... i.e. i make a Ext.define('MyNewModel' {..., fields: myFields}); .. the model is of course extending a former class with all functionality e.g. proxy and reader configuration i needed. The only thing left, was the fields configuration. Unfortunately I didn't find a way to put them to the class definition after the define and before a create. One may override the base-class and look for some global variable to put into the fields... but.. erm, no thanks.

After defining the model you can create a store with passing the model and give a storeId to it... after that it should work... - I did that with a dynamic grid component and it worked...

kislay.kishore
3 Feb 2012, 12:45 AM
I underdtand your reposnse to an extend, but not completely. Can you please elaborate with some samlpe code.

Thanks a lot! Appreciate the help!


So I think you want to build a store dynamically for some kind of metadata sent by the server. There are some threads around discussing that problem I think? I did this today too.. just by defining my model inline... i.e. i make a Ext.define('MyNewModel' {..., fields: myFields}); .. the model is of course extending a former class with all functionality e.g. proxy and reader configuration i needed. The only thing left, was the fields configuration. Unfortunately I didn't find a way to put them to the class definition after the define and before a create. One may override the base-class and look for some global variable to put into the fields... but.. erm, no thanks.

After defining the model you can create a store with passing the model and give a storeId to it... after that it should work... - I did that with a dynamic grid component and it worked...

kislay.kishore
23 Aug 2012, 5:38 AM
By assuming that the the JSON response from backend comes as JSON ArrayList and each of its elements can be considered as one record.

Below is the solution, it may not be applied to all the cases, but with the assumption it should work fine.

Here is the solution:



/* getFieldNames : A function to take JSON Array as input and return the keys of the first object as output
* Input Params
* 1. jsonArray : This is a JSON Array
** Output param
* fieldNames: This is a string array (keys of the first object as output)
**/
function getFieldNames(jsonArray) {

var jsonStr = eval( jsonArray );
var p = jsonStr[0];
var fieldNames = new Array();
var orderedfieldNames = new Array();

var counter = 0;

for (var key in p) {
if (p.hasOwnProperty(key)) {
if(key == "Month" || key == "Week" || key == "Year"){
orderedfieldNames[0] = key;
}else{
fieldNames[counter] = key;
counter++;
}
}
}

fieldNames.sort();
orderedfieldNames = orderedfieldNames.concat(fieldNames);
return orderedfieldNames;
}

function createStore(fieldsStringArray, jsonDataString){

var jsonStr = eval('(' + jsonDataString + ')');
var chartStore= Ext.create('Ext.data.Store', {
fields: fieldsStringArray,
data : jsonStr
});
return chartStore;
}

var fieldNames = getFieldNames(reportJson);
myStore = createStore(fieldNames,reportJson );