1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    5
    Vote Rating
    0
    Tap Into Apps is on a distinguished road

      0  

    Default ST2B1: Localstorage store won't sync

    ST2B1: Localstorage store won't sync


    Hi,

    I have been attempting to migrate one of my ST1 apps over to ST2B1 and all though it has been a bit of a learning curve to get used to it, I have hit a stopping point.

    I am trying to sync an online ajax/json store to an offline localstorage store onStoreLoad using the same method I used in ST1. However when I try to sync the store to localstorage I get no errors in the console and nothing is saved. My code is:

    Code:
            store.each(function (record) {
                var offer = offlineOffers.add(record.data)[0];
                console.log(offer);
            });
            offlineOffers.sync();
            console.log(offlineOffers);
    My store is configured as:

    Code:
    Ext.define('tia.store.OfflineOffers', {
        extend: 'Ext.data.Store',
        requires: ['tia.model.Offer'],
        config: {
            model: 'tia.model.Offer',
            proxy: {
                type: 'localstorage',
                id: 'TIA-AFF-Offer'
            }
        }
    });
    I can see in the Chrome debugger that "var offer" is populated each time, and also that the data property of the store has an array of the 16 items in it afterwards. The _totalCount value is still at 0 though.

    As there are no errors logged I don't know what to try next so would be grateful of some help.

    Thanks!

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,641
    Vote Rating
    898
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    The following code seems to be working just fine in our latest internal code that will be part of the beta2 release:

    Code:
    Ext.define('MyStore', {
        extend : 'Ext.data.Store',
    
        config : {
            fields : ['text'],
            proxy  : {
                type : 'localstorage',
                id   : 'Test'
            }
        }
    });
    
    Ext.setup({
        onReady : function() {
    
            var store = new MyStore();
            
            store.add({
                text : 'One'
            });
            
            store.sync();
    
        }
    });
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  3. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    5
    Vote Rating
    0
    Tap Into Apps is on a distinguished road

      0  

    Default


    I forgot to add that if I create an object as you have done and then sync it, it actually saves to the localstorage. The problem only seems to occur when using the store with a model, and trying to add instances of those models to the store and then syncing.

    Here is my model:

    Code:
    Ext.define('tia.model.Offer', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                { name: 'offer_header', type: 'string' },
                { name: 'discount_header', type: 'string' },
                { name: 'offer_description1', type: 'string' },
                { name: 'offer_description2', type: 'string' },
                { name: 'list_description', type: 'string' },
                { name: 'product_heading', type: 'string' }
            ]
        }
    });
    I did previously have an id field on here but after reading another thread decided to remove it, but this hasn't made any difference.

  4. #4
    Sencha User
    Join Date
    Feb 2011
    Posts
    74
    Vote Rating
    3
    shaneavery is on a distinguished road

      0  

    Exclamation I can duplicate this

    I can duplicate this


    I concur with Tap Into Apps.

    My Model class:
    Code:
    Ext.define('dwila.model.Guest', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                    { name: 'id', type: 'auto' },
                    { name: '_id', type: 'string' },
                    { name: 'Party', type: 'int' },
                    { name: 'Name', type: 'string' },
                    { name: 'Phone', type: 'string' },
                    { name: 'Carrier', type: 'string' },
                    { name: 'Tags', type: 'string' },
                    { name: 'Sent', type: 'boolean', defaultValue: false },
                    { name: 'Serviced', type: 'boolean', defaultValue: false },
                    { name: 'Hide', type: 'boolean', defaultValue: false },
                    { name: 'Item', type: 'int', defaultValue: 0 },
                    { name: 'date_reserved', type: 'date', defaultValue: new Date(), dateFormat: 'c' },
                    { name: 'date_sent', type: 'date', dateFormat: 'c' },
                    { name: 'date_serviced', type: 'date', dateFormat: 'c' },
                    { name: 'sent_count', type: 'int', defaultValue: 0 }
            ],
            proxy: {
                type: 'localstorage',
                id: 'dwilaguest'
            },
            validations: [
                {type: 'format',   name: 'Party', matcher: /\d+/, message: 'must only include numeric characters'},
                {type: 'presence', name: 'Name', message: 'field is required'},
                {type: 'length',   name: 'Phone', min: 10, max: 10, message: 'numbers must be exactly 10 digits'},
                {type: 'format',   name: 'Phone', matcher: /\d+/, message: 'must only include numeric characters'},
                {type: 'format',   name: 'Item', matcher: /\d+/, message: 'must only include numeric characters'}
            ]
        }
    });
    My store class:
    Code:
    Ext.define('dwila.store.Guests', {
        extend: 'Ext.data.Store',
        requires : ['dwila.model.Guest'],
        config: {
            model: 'dwila.model.Guest',
            autoSync: true,
            sorters: [
                {
                    property : 'date_reserved',
                    direction: 'DESC'
                }
            ],
            autoLoad: true
        }
    });
    My relevant implementation in my controller class:
    Code:
        onGuestAddBtn: function() {
    
            var guest = Ext.ModelMgr.create({
                        '_id': UUID.generate(),
                        'Party': 1,
                        'Name': '',
                        'Phone': '1111111111',
                        'Carrier': '',
                        'Tags': '',
                        'Sent': false,
                        'Serviced': false,
                        'Item': 1,
                        'date_reserved': new Date(),
                        'date_sent': null,
                        'date_serviced': null,
                        'sent_count': 0
            }, 'dwila.model.Guest');
    
            var gridStore = Ext.getStore('Guests');
            //gridStore.add(guest); // behaves normally
            gridStore.insert(0, guest); // behaves normally
            gridStore.sync(); // no error thrown in console
            gridStore.load(); // store empty, model instance not saved to local storage
            
            console.log(Ext.data.StoreManager.lookup('Guests').getAt(0).get('_id')); // returns "Uncaught TypeError: Cannot call method 'get' of undefined"
    
            var edit = this.getEdit() || Ext.create('dwila.view.Edit');
            edit.setRecord(guest);
            edit.show();
            this.setBusinessMode('form');
        },
    The model "Guest" simply is not writing to local storage. I might add I am using more than one store with this app, but it does not seem to matter.

    Thanks

  5. #5
    Sencha Premium Member
    Join Date
    Oct 2011
    Location
    San Antonio, TX
    Posts
    93
    Vote Rating
    0
    jnotti is on a distinguished road

      0  

    Exclamation ST2B2 Ditto...

    ST2B2 Ditto...


    Same issue with ST2 B2... see these threads (no resolve):