1. #1
    Sencha User
    Join Date
    Aug 2012
    Posts
    91
    Answers
    6
    Vote Rating
    0
    alex9311 is on a distinguished road

      0  

    Default Answered: Creating store from another store

    Answered: Creating store from another store


    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:

    Code:
            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.

  2. 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:

    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

  3. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,018
    Answers
    467
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      1  

    Default


    You're adding an extra array:

    Code:
    storeArray.push([{"grade":item.get('grade'),"firstaction":item.get('firstaction')}]);
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  4. #3
    Sencha User
    Join Date
    Aug 2012
    Posts
    91
    Answers
    6
    Vote Rating
    0
    alex9311 is on a distinguished road

      0  

    Default


    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:

    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

Thread Participants: 1

Tags for this Thread