1. #1
    Sencha User
    Join Date
    May 2012
    Posts
    3
    Vote Rating
    0
    weelion is on a distinguished road

      0  

    Default Unanswered: How can i insert data to localstorage

    Unanswered: How can i insert data to localstorage


    store/Users.js
    Code:
    Ext.define('MyApp.store.Users', {
        extend: 'Ext.data.Store',
        requires: "Ext.data.proxy.LocalStorage",
        config: {
            storeId: 'userstore',
            model: 'MyApp.model.User',
            proxy: {
                type: 'localstorage',
                id : 'user-data',
            },
        }
    });
    model/User.js
    Code:
    Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',
        config: {
            idProperty: 'id',
            fields: [
                { name: 'id', type: 'int' },
                { name: 'username', type: 'string' },
                { name: 'password', type: 'string' },
            ],
            validations: [
                { type: 'presence', field: 'id' },
                { type: 'presence', field: 'username' },
                { type: 'presence', field: 'password' }
            ]
        }
    });
    app.js
    Code:
        .........
    
        launch: function() {
            console.log("App launch");
            var store = Ext.getStore('userstore');
    
    
            var data = {'id': '1','username': 'the username', , 'password': 'the password'};
    
    
            store.add(data);
            store.sync();
    
    
            store.load().each(function(record) {
                console.log(record.getData());
            });
    
    
            console.log('Count: ' + store.getCount());             // output 1
            console.log('Load Count: ' + store.load().getCount()); // output 0
    
        }
        .......
    When I run this. the localstorage is empty. Is there something wrong.

    But, when I change the data to ...
    Code:
    var data = {'username': 'the username', , 'password': 'the password'};
    It works.

    Object
    • id: "ext-record-1"
    • password: "the password"
    • username: "the username"
    • __proto__: Object
    but get a warning.

    [WARN][Ext.data.Batch#runOperation] Your identifier generation strategy for the model does not ensure unique id's. Please use the UUID strategy, or implement your own identifier strategy with the flag isUnique.

    I search in google and this forum.No one got this problem.

  2. #2
    Sencha User
    Join Date
    Dec 2011
    Location
    Sacramento, California
    Posts
    75
    Vote Rating
    9
    gypsie will become famous soon enough

      0  

    Default


    They have bug in 2.0.1 localstorage.

    read this:
    http://www.sencha.com/forum/showthre...ete-EVERYTHING

    I ended up to follow the suggestion in that link and set "syncRemovedRecords: false," in store settings, set localstorage proxy in model too, and then used model's 'save' for initial insert and then kept store's sync for updates (with that syncRemovedRecords set to false). Done this way it does what I need it to do, to create and update one and only record with the same id:1, and it doesn't generate the new uuid warning.


    Code:
            settingsstore = Ext.getStore('SettingsStore');
            settings = settingsstore.getById(1);
             
            
    
    
            if ( settings == null ) {
            // create
            
                var model = new WP.model.SettingsModel({
                    id:    '1',
                    current_user : 'Foo'
                });
                var records = settingsstore.add(model);
                records[0].save();
            
            } else {
            // update
                
                settings.set(name,value);
           
            }
            
            // sync
            settingsstore.sync();
    Code:
    Ext.define('SettingsModel', {
        extend: 'Ext.data.Model',
    
    
        config: {
        //identifier: 'uuid',
            idProperty: 'id',
            fields: [
                {name: "id", type: "auto" }, // isUnique: true
                {name: "current_user", type: "string"},
            ],
            
                proxy: {
                type: 'localstorage',
                id  : 'th-settings'
            },
    
        }
        
    });
    Code:
    Ext.define('SettingsStore', {
        extend: 'Ext.data.Store',
    
    
        config: {
            model: 'SettingsModel',
            syncRemovedRecords: false,
            
                proxy: {
                type: 'localstorage',
                id  : 'th-settings'
            },
            
    
    
        }
        
    });
    Last edited by gypsie; 17 May 2012 at 1:43 PM. Reason: typo

  3. #3
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      2  

    Default


    I added in my model config identifier:'uuid' and the warning is gone.

  4. #4
    Sencha User Skynet-tx's Avatar
    Join Date
    May 2012
    Location
    Sevastopol, Russia
    Posts
    20
    Vote Rating
    1
    Skynet-tx is on a distinguished road

      1  

    Default My example of code

    My example of code


    If you do this on Sencha Architect or simple on Sencha. And you made model and store as done above. I show my instance of code. This code really simple and works.
    Code:
     launch: function() {       
            var storePlayers =  Ext.getStore('players');
            var playerData = [
            { "id": 1, "uniform": 1, "first": "Player1", "last": "", "short": "Player1", "pos": "1"},
            { "id": 2, "uniform": 2, "first": "Player2", "last": "", "short": "Player2", "pos": "2"},
            { "id": 3, "uniform": 3, "first": "Player3", "last": "", "short": "Player3", "pos": "3"},
            { "id": 4, "uniform": 4, "first": "Player4", "last": "", "short": "Player4", "pos": "4"},
            { "id": 5, "uniform": 5, "first": "Player5", "last": "", "short": "Player5", "pos": "5"},
            { "id": 6, "uniform": 6, "first": "Player6", "last": "", "short": "Player6", "pos": "6"},
            { "id": 7, "uniform": 7, "first": "Player7", "last": "", "short": "Player7", "pos": "7"},
            { "id": 8, "uniform": 8, "first": "Player8", "last": "", "short": "Player7", "pos": "8"}
            ];
            var playerRow = storePlayers.getRange();
    
    
            // initialize the database when you first logon
            if (Ext.isEmpty(playerRow)){
                if (!confirm('Are you sure you want to add demo player data?'))  return;
                var recods =  storePlayers.add(playerData);
                for(var key in playerData)
                {
                    //                var val = playerData[key];
                    recods[key].save();
                }
            }
        }