PDA

View Full Version : Creating store from another store



alex9311
30 Jan 2013, 3:27 PM
Hey all,

I'm trying to take a store that has letter grades ( "A", "B",etc..) and create a new store that has percentages (95,85,etc). I think I almost have it working but something isnt quite right. Here is my function:


createStore:function(){
//console.log("in the create store function");
var store = Ext.data.StoreManager.lookup('Plot');
var storeArray = [];
Ext.each(store.getRange(), function (item, idx, a) {
//console.log(item.get('grade'));
storeArray.push([{"grade":item.get('grade'),"firstaction":item.get('firstaction')}]);
});
//console.log(storeArray);
var newStore = Ext.create('Ext.data.Store',{
model: 'PL.model.NewPlot',
fields: [{
name: 'grade', convert: function(value,record){
//console.log(value);
//console.log(record);
if (value == 'A'){
return 95;
}if (value == 'B'){
return 85;
}if (value == 'C'){
return 75;
}if (value == 'D'){
return 65;
}if (value == 'F'){
return 55;
}
}
},{
name: 'firstaction', mapping: 'firstaction'
}],
data: storeArray
});
return newStore;
},

Note: Both stores have a 'grade' field and a 'firstaction' field. I just want to take the string grade field from the first store and make it an int field. The firstaction field should just copy over.

evant
30 Jan 2013, 4:00 PM
You're adding an extra array:



storeArray.push([{"grade":item.get('grade'),"firstaction":item.get('firstaction')}]);

alex9311
30 Jan 2013, 4:11 PM
Thanks! Once I started looking more into my array I realized I could just convert the strings to ints when I construct that array and map it directly. Here is the working code:


createStore:function(){
var store = Ext.data.StoreManager.lookup('Plot');
var storeArray = [];
Ext.each(store.getRange(), function (item, idx, a) {
var oldgrade = item.get('grade');
var newgrade = "";
if (oldgrade == 'A'){
newgrade = 95;
}if (oldgrade == 'B'){
newgrade = 85;
}if (oldgrade == 'C'){
newgrade = 75;
}if (oldgrade == 'D'){
newgrade = 65;
}if (oldgrade == 'F'){
newgrade = 55;
}
storeArray.push({"grade":newgrade,"firstaction":item.get('firstaction')});
});
var newStore = Ext.create('Ext.data.Store',{
model: 'PL.model.NewPlot',
fields: [{
name: 'grade', mapping: 'grade'
},{
name: 'firstaction', mapping: 'firstaction'
}],
data: storeArray
});
return newStore;
},

Of course any comments on efficiency and coding style are appreciated