Threaded View

  1. #1
    Sencha Premium Member TheStreet's Avatar
    Join Date
    Sep 2012
    Posts
    63
    Answers
    2
    Vote Rating
    0
    TheStreet is on a distinguished road

      0  

    Default Answered: Can't Access Any Dynamically Created Stores

    Answered: Can't Access Any Dynamically Created Stores


    So I'm working on an app that receives information from a web service, and based on that information I need to created local and remote data stores. I'm using shepsii's SQLite proxy which is working great, but I'm having trouble accessing stores that I'm creating on the fly. Maybe someone can check out my code and see if there's something I'm missing. If I try to Ext.getStore(storeName) it comes back undefined.

    Code:
    Ext.define('TheStreet.controller.Main', {
        extend: 'Ext.app.Controller',
        requires: ['Ext.DateExtras'],
        config: {
            refs: {
                viewport        : 'viewport',
                newsmenu        : 'newsmenu',
                newsfeed        : 'newsfeed',
                updated         : '#updatedOn',
                widgetPanel     : '#widgetpanel',
                articleCarousel : 'articleCarousel'
            },
            control: {
                viewport: {
                    ready: 'launch'
                }
            },
        },
    
    
        init: function() {
            this.callParent();
            
            this.sectionStore             = Ext.getStore('Sections');
            this.sectionSyncStore         = Ext.getStore('SectionSync');
            this.lastUpdatedStore         = Ext.getStore('LastUpdated');
        },
            
        launch: function() {
            var me = this;
    
    
            me.createSectionStores();
        },
    
    
        createSectionStores: function() {
            this.sectionSyncStore.load({
                callback: function(records, operation, success) {
                    this.sectionStore.getProxy().truncate('sections');
    
    
                    this.sectionSyncStore.each(function(record) {
                        var rec = Ext.create('TheStreet.model.Section', record);
                        rec.save();
                    });
    
    
                    this.sectionStore.load({
                        callback: this.onSectionCreation,
                        scope: this
                    });
                },
                scope: this
            });
        },
    
    
        onSectionCreation: function(records, operation, success) {
            console.log('section store', this.sectionStore);
            console.log('section store count', this.sectionStore.getCount());
    
    
            this.sectionStore.each(function(rec, index, numItems) {
                var name = rec.data.name.replace(/\s/g, ''),
                    source = rec.data.source;
    
    
                this.createNewsModel(name);
                this.createNewsSyncStore(name, source);
                this.createNewsStore(name);
                this.syncStore(name);
            }, this);
        },
    
    
        createNewsModel: function(modelName) {
            return Ext.define(modelName, {
                extend: 'Ext.data.Model',
                config:{
                    fields: [
                        {name: 'id',            type: 'int'},
                        {name: 'articleId',     type: 'int'},
                        {name: 'headline',      type: 'string'},
                        {name: 'body',          type: 'string'},
                        {name: 'callout',       type: 'string'},
                        {name: 'byLine',        type: 'string'},
                        {name: 'smallThumb',    type: 'string'},
                        {name: 'largeThumb',    type: 'string'},
                        {name: 'industry',      type: 'string'},
                        {name: 'category',      type: 'string'},
                        {name: 'subcategory',   type: 'string'},
                        {name: 'publishDate',   type: 'string'},
                        {name: 'tickers',       type: 'string'}
                    ],
                    proxy: {
                        type: 'sqlitestorage',
                        dbConfig: {
                            tablename: modelName,
                            dbConn: TheStreet.util.InitSQLite.getConnection()
                        },
                        reader: {
                            type: 'array'
                        }
                    }
                }             
            });
        },
    
    
        createNewsSyncStore: function(storeName, proxyURL) {
            return Ext.define(storeName+'Sync', {
                extend: 'Ext.data.Store',
                config:{
                    model: storeName,
                    proxy: {
                        type: 'ajax',
                        url: proxyURL,
                        extraParams: {
                            max: '15',
                            contentFmt: 'ipad_html'
                        },
                        reader: {
                            type: 'json',
                            rootProperty: 'response.articles'
                        }
                    }
                }             
            });
        },
    
    
        createNewsStore: function(storeName) {
            return Ext.define(storeName, {
                extend: 'Ext.data.Store',
                config:{
                    model: storeName,
                    storeId: storeName,
                    autoLoad: true,
                    pageSize: 1000,
                    sorters: [
                        {
                            property : 'publishDate',
                            direction: 'DESC'
                        }
                    ]
                }             
            });
        },
    
    
        syncStore: function(name) {
            var store = Ext.getStore(name),
                syncStore = Ext.getStore(name+'Sync'),
                stores = Ext.data.StoreManager.getRange();
    
    
            console.log(name);
            console.log(stores);
            console.log(Ext.isDefined(syncStore));
    
    
            syncStore.load({
                callback: function(records, operation, success) {
                     console.log('loading top news');
                    // Clear out the offline people list
                    store.getProxy().truncate(store.getId());
    
    
                    syncStore.each(function(record) {
                        var rec = Ext.create('TheStreet.model.'+name, record);
                        rec.save();
                    });
                    store.load(); // Reload store
                },
                scope: this
            });
        }
    
    
    });

  2. So I swapped out those Ext.defines on the Stores for this:

    Code:
    createNewsSyncStore: function(storeName, proxyURL) {
            return Ext.create('Ext.data.Store', {
                model: storeName,
                storeId: storeName+'Sync',
                proxy: {
                    type: 'ajax',
                    url: proxyURL,
                    extraParams: {
                        max: '15',
                        contentFmt: 'ipad_html'
                    },
                    reader: {
                        type: 'json',
                        rootProperty: 'response.articles'
                    }
                }         
            });
        },
    
    
        createNewsStore: function(storeName) {
            return Ext.create('Ext.data.Store', {
                model: storeName,
                storeId: storeName,
                autoLoad: true,
                pageSize: 1000,
                sorters: [
                    {
                        property : 'publishDate',
                        direction: 'DESC'
                    }
                ]        
            });
        },

Thread Participants: 1

Tags for this Thread