1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    3
    Vote Rating
    0
    lovemyway is on a distinguished road

      0  

    Default Unanswered: How can I access a store in an utility class?

    Unanswered: How can I access a store in an utility class?


    Hello,

    I've been developing some application using Sencha Touch 2 and I've encountered a little problem - I got a model:
    Code:
    Ext.define('STI.model.NotecaseMenu', {    extend: 'Ext.data.Model',
        
        config: {
            fields: [
                { name: 'fund', type: 'string' },
                { name: 'subfund', type: 'string' },
                { name: 'balance', type: 'float' },
                { name: 'registryNumber', type: 'string' },
                { name: 'productType', type: 'string' },
                { name: 'currency', type: 'string' },
                { name: 'distributor', type: 'string' },
                { name: 'openDate', type: 'string' },
                { name: 'registryState', type: 'string' },
                { name: 'unitValue', type: 'float' },
                { name: 'loss', type: 'float' },
                { name: 'bankAccount', type: 'string' }
            ]
        }
    });
    ,

    and a store:
    Code:
    Ext.define('STI.store.NotecaseMenuStore', {    extend: 'Ext.data.Store',
        
        config: {
            model: 'STI.model.NotecaseMenu',
            storeId: 'notecaseMenuStore',
            
            data: [
                { fund: 'Company FIO', subfund: 'AKCJA', balance: 5000.0, registryNumber: '700-025-689', productType: 'rejestr zwykły', currency: 'PLN', distributor: 'ProService AT',  openDate: '2006-11-14', registryState: '975,773 j.u.', unitValue: 329.00, loss: 222466.42, bankAccount: '26 1140 1010 7020 0002 0002 0000' },
                { fund: 'Company FIO', subfund: 'AKCJA', balance: 1544.0, registryNumber: '700-895-623', productType: 'rejestr zwykły', currency: 'PLN', distributor: 'ProService AT',  openDate: '2006-11-14', registryState: '5,53 j.u.', unitValue: 329.00, loss: 222466.42, bankAccount: '26 1140 1010 7020 0002 0002 0000' },
                { fund: 'Company FIO', subfund: 'WAGA', balance: 2045.0, registryNumber: '400-012-300', productType: 'rejestr zwykły', currency: 'PLN', distributor: 'ProService AT',  openDate: '2006-11-14', registryState: '75,72 j.u.', unitValue: 329.00, loss: 222466.42, bankAccount: '26 1140 1010 7020 0002 0002 0000' },
                { fund: 'Company FIO', subfund: 'WAGA', balance: 4288.0, registryNumber: '400-047-950', productType: 'rejestr zwykły', currency: 'PLN', distributor: 'ProService AT',  openDate: '2006-11-14', registryState: '773,554 j.u.', unitValue: 329.00, loss: 22246.642, bankAccount: '26 1140 1010 7020 0002 0002 0000' },
                { fund: 'Company FIO', subfund: 'WAGA', balance: 1234.5, registryNumber: '400-047-950', productType: 'rejestr zwykły', currency: 'PLN', distributor: 'ProService AT',  openDate: '2006-11-14', registryState: '773,554 j.u.', unitValue: 329.00, loss: 22246.642, bankAccount: '26 1140 1010 7020 0002 0002 0000' }
            ]
        }
    });
    Now, I want to write an utility function that will sum all the balance fields from the store and return a result. I was trying to write a function, and then get a store with Ext.getStore('notecaseMenuStore'), but it says it's undefined.
    Code:
    notecaseValue = function(){
        var store = Ext.getStore('notecaseMenuStore');
        var count = store.getCount();
        var sum = 0;
    
    
        for(i = 0; i < count; i++)
        {
            sum += store.getAt(i).data.balance;
        }
        
        return sum;
    }
    Where did I go wrong? Is there another way around? Should I define that utility class somehow?

    Regards,
    lovemyway

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,714
    Vote Rating
    438
    Answers
    3113
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Ext.getStore will work if the store has been created once somewhere either in the stores array in Ext.application or in a widget.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Dec 2011
    Posts
    3
    Vote Rating
    0
    lovemyway is on a distinguished road

      0  

    Default


    I have my store listed in Ext.application section and still got this problem. The problem disappears if I put a check in my util class:
    Code:
    var store = Ext.getStore('notecaseStore');
    if(!store) {
    store = Ext.create('STI.store.NotecaseStore');
    }
    but then the store is created two times (i was listening to load event in store and alert popped out twice), so I guess it's not right.

    I'm trying to use this function in normal view class (defined in Ext.application too), in a panel (html: 'amount' + notecaseValue()). Is there any other way?