Search code examples
sencha-touch-2sencha-architect

sencha touch insert data to localstorage


Hi i have found a lot of examples about loading data from a db in sencha. i try to make a list with notes and on second step i want to be able to add(save) a note to my db. i try that on localstorage.

for now i load data from a array in my Arraystore. where shall i set my proxy? (in store or in model?)

how could i insert data in my store? i have tried something like that on my current arraystore but with no luck: (this is the code run by pressing a code):

 MyArrayStore.add({title:"newnote",narrative:"bla bla bla",date:now,id:noteid});
    MyArrayStore.sync();

the browser console gets an error: Uncaught ReferenceError: MyArrayStore is not defined shall i make an instance of my store or something?

my model is this: thanx for the answer. i try that on architect. my model is this:

Ext.define('MyApp.model.NoteModel', {
    extend: 'Ext.data.Model',
    alias: 'model.NoteModel',
    config: {
        fields: [
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'date',
                type: 'date'
            },
            {
                name: 'title',
                type: 'string'
            },
            {
                name: 'narrative',
                type: 'string'
            }
        ],
        proxy: {
            type: 'localstorage',
            id: 'local'
        }
    }
});

and my store is this:

Ext.define('MyApp.store.MyArrayStore', {
    extend: 'Ext.data.Store',
    requires: [
        'MyApp.model.NoteModel'
    ],

    config: {
        data: [
            {
                title: 'Note 1',
                narrative: 'test1 1'
            },
            {
                title: 'Note 2',
                narrative: 'narrative 2'
            },
            {
                title: '3 ertyyh',
                narrative: 'narrative 3'
            },
            {
                title: '4 asdf',
                narrative: 'narrative 4'
            },
            {
                title: 'Note 5',
                narrative: 'narrative 5'
            },
            {
                title: 'weadf',
                narrative: 'narrative 6'
            }
        ],
        model: 'MyApp.model.NoteModel',
        storeId: 'MyArrayStore'
    }
});

Solution

  • You should set your proxy in your model OR in your store. Here's how to do it within your model.

    Ext.define('MyModel', {
    
    extend: 'Ext.data.Model',
    
    config: {
        fields: ['field1'],
        proxy: {
            type: 'localstorage',
            id  : 'my-model-localstorage-id'
        }
    });
    

    The same can alternatively be done in your store.

    After that, given that 'MyArrayStore' is an instance of such a store, the code you propose should work just fine.

    Hope this helps.