javascriptjsonextjsformulasextjs-stores

Ext JS: How to get sum of two different JSON store's values?


As you will notice belowe I'm fetching to two different stores through formulas and getting TOTALCOUNT integer values of those JSONs.

How can I get sum of those two different stores?

formulas: {
        firstStore: {
            bind: {
                bindTo: '{firstStatStore}',
                deep : true
            },
            get: function (store) {
                var record = store.findRecord(MyApp.Names.CODE, MyApp.Names.TOTALSTAT);
                return record ? record.get(MyApp.Names.TOTALCOUNT) : "-1";
            }
        },

        secondStore: {
            bind: {
                bindTo: '{secondStatStore}',
                deep : true
            },
            get: function (store) {
                var record = store.findRecord(MyApp.Names.CODE, MyApp.Names.TOTALSTAT);
                return record ? record.get(MyApp.Names.TOTALCOUNT) : "-1";
            }
        },

       thirdStore: {
                // Here I need "sum of TOTALCOUNT" from firstStore and secondStore.
       }       
    }

Solution

  • You can bind to formulas the same as you can with anything else:

    thirdStore: function(get) {
        return get('firstStore') + get('secondStore');
    }
    

    Also, you should change the return type in your other formulas, not sure why you would return a string there.