PDA

View Full Version : How to read the store variable from another function



ppatel2
4 Jun 2013, 5:42 AM
I have a Json store where I am returning some extra variables. I am trying to set the dynamic columns. For that I need to read the variable "isAdminUser" in the grid and pass it on to the function to set the dynamic columns. The variable returns "undefined". How can I read the value of this variable?


var store = new Ext.data.JsonStore( { fields: [
'pbid','playbook','status','company'
],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'sortAjax.do?action=playbookJson',
reader: {
type: 'json',
root: 'Results',
totalProperty: 'Count',
isAdminUser: 'isAdminUser'
}
}
});

Now I want to read the variable from a function

function getColumnDef(isAdminUser){ var columnDef = [];
if(isAdminUser){
columnDef= [
{text: "Playbook", flex:1, dataIndex: 'playbook', sortable: true},
{text: "Status", flex:0.5, dataIndex: 'status', sortable: true},
{text: "Company", flex: 2.5, dataIndex: 'company', sortable: true}
];
}
else{
columnDef = [
{text: "Playbook", flex:1, dataIndex: 'playbook', sortable: true},
{text: "Company", flex: 2.5, dataIndex: 'company', sortable: true}
]
}
return columnDef;
}


var playbookGrid = new Ext.grid.GridPanel({ store: store,
width: '100%',
scroll: 'false',
columns: getColumnDef(store.isAdminUser),
plugins : expander,
viewConfig : {
listeners : {
expandbody: function(rowEl, record, bodyEl, rowIndex) {
var myNode = document.getElementById('BarChart-' + record.get('pbid'));
myNode.innerHTML = '';
myNode = document.getElementById('GaugeChart-' + record.get('pbid'));
myNode.innerHTML = '';
this.rowBodyTpl = new Ext.Template(getChart('BarChart-' + record.get('pbid'),'GaugeChart-' + record.get('pbid')));
}
}
}
});

Thanks,
Parth

vvorobeychik
4 Jun 2013, 9:56 AM
There are couple solutions:

1. The store has config option "storeId". Use Ext.getStore(storeId value). See documentation.
2. Declare module level variable, load and assign store to it and use this variable in any function in the module.

slemmon
6 Jun 2013, 11:01 AM
Are you meaning to put the isAdminUser property on the store or its reader?



Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
isAdminUser: 'isAdminUser',
fields:['name', 'email', 'phone'],
data:{'items':[
{ 'name': 'Lisa', "email":"lisa@simpsons.com", "phone":"555-111-1224" },
{ 'name': 'Bart', "email":"bart@simpsons.com", "phone":"555-222-1234" },
{ 'name': 'Homer', "email":"home@simpsons.com", "phone":"555-222-1244" },
{ 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254" }
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});


var grid = Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});


console.log(grid.getStore().isAdminUser);

ppatel2
6 Jun 2013, 11:26 AM
Hi Slemmon,

I am trying to set the value of the field by passing it in the json object like we pass totalCount, root, etc. and assign them in the reader. Is it possible to set the field in this manner ?

Thanks,
Parth

slemmon
6 Jun 2013, 2:14 PM
I see. Try with grid.getStore().getProxy().getReader().rawData
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Reader-property-rawData