Question

Using sencha touch, I want to store my array list data in localStorage as shown in image.

enter image description here

onStore refresh function as shown in image below

enter image description here

I stored simple data as Like Date, Id, Name in LocalStorage on BookStore refresh function. But could not find the way to store arrayObject directly to localStorage.

Was it helpful?

Solution

I am not sure i understand your question completely..

If you want store javascript object in local Store, you can do it by converting javascript object into string using JSON.stringify() method.

var testString = JSON.stringify(testObj); //Converts testObject to Json string.
localStorage.testString = testString; // Stores testString into local storage.

You can get testString from local storage and convert it into javascript object by

var testObj = JSON.parse(localStorage.testString);

Or you can use

Ext.encode(); // Encodes an Object, Array to String.

Ext.decode(); // Decodes (parses) a JSON string to an object.

OTHER TIPS

you can store full object but if you just want to store subjects then you need to create new model. define a model for storing subjects or you can store the full object you are getting.

Ext.define('Demo.model.Subjects', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'id', type: 'int' },
            { name: 'Subjects', type: 'auto' }
        ]
    }
});

define a local store

Ext.define('Demo.store.MySubjectLocalStore', {
    extend: 'Ext.data.Store',
    config: {
        storeId: 'mySubjectLocalStore',
        model: 'Demo.model.Subjects',
        autoLoad: true,
        clearOnPageLoad: true,
        proxy: {
            type: 'localstorage',
            id: 'demo-mySubjects-local-store'
        }
    }
});

to store data in local store

var subjectsLocal = Ext.getStore('mySubjectLocalStore');

//loop to get your subjects array from main Object then add each array to localstore 
//loop start
var subjects = //get it from parent object inside loop
var subjectsModel = Ext.create('Demo.model.Subjects', {
    Subjects : subjects,
    id: //use integer so that you can get by this id when you retrive  it
});

subjectsLocal.add(subjectsModel);
//loop End

subjectsLocal.sync();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top