Replace a Sencha Touch 2 store which always stores only one model, with a model linked directly to SessionStorage

StackOverflow https://stackoverflow.com/questions/23300743

  •  09-07-2023
  •  | 
  •  

Question

I have defined the following Sencha Store, which uses SessionStorage:

Ext.define('TestApp.store.SessionStore', {
    extend: 'Ext.data.Store',    
    config: {
        model: 'TestApp.model.SessionStoreModel',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'sessionstorage',
            id: 'SessionStore'
        }
    }
});

This store has the following model:

Ext.define('TestApp.model.SessionStoreModel', {
    extend: 'Ext.data.Model',
    config:{
        identifier: {
            type: 'uuid'
        },
        fields: [
            { name:'token', type:'string'},
            { name:'auth', type:'boolean', defaultValue: false }
        ]
    }
});

The ideas is,

I will always have just one model inside, not more.

It seems an overkill to have a store for just one model (i have to clear it, count it's index when using it).

In my humble opinion, there should be a way to make the model save data directly to SessionStorage and reference it, not evolving a store.

Can this be done in Sencha Touch 2?

UPDATE

Here is what I did with no avail:

Ext.define('TestApp.model.TestModel', {
    extend: 'Ext.data.Model',
    config: {
        identifier: {
            type: 'uuid'
        },
        fields: [
            { name:'auth', type:'boolean', defaultValue: false }
        ],
        proxy: {
            type: 'sessionstorage'
        }
    }
});

Save model data:

// Test.

var testModel = Ext.create('TestApp.model.TestModel', {
            auth: 'La Playa'
        });

        testModel.save({
            scope: this,
            success: function() {
                console.log('TestModel success');
            },
            failure: function() {
                console.log('TestModel failure');
            },
            callback: function() {
                console.log('TestModel callback');
            }
        });

Reade model data:

 // Test.
        TestApp.model.TestModel.load(1, {
            scope: this,
            success: function(record, operation) {
                console.log('TestModel read success');
                console.log(record.get('auth'));
            },
            failure: function(record, operation) {
                console.log('TestModel read failure');
            },
            callback: function(record, operation) {
                console.log('TestModel read callback');
            }
        });

But it does not work, cannot read from SessionStorage

Was it helpful?

Solution

This can be done in Sencha Touch 2.

Ext.define('TestModel', {
    extend: 'Ext.data.Model',
    config: {
        identifier: {
            type: 'uuid'
        },
        fields: [
            {name: 'token', type: 'string'},
        ],
        proxy: {
            type: 'sessionstorage'
        }
    }
});

var testModel = Ext.create('TestModel', {
    token: 'Token'
});

var id;
testModel.save(function (record) {
    id = record.getId();
    console.log('id:' + record.getId());
});

TestModel.load(id, {
    failure: function (record, operation) {
        console.log('Failed...');
    },
    success: function (record, operation) {
        console.log('It worked');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top