wmarbut
wmarbut

Reputation: 4685

Sencha Touch (1.1) - Model not updated in localstorage after "successful" save

My Setup

The Issue

The process

I have a model and a store. I update a record using the set(field,value) method documented here: API - Set. After updating fields, I use save() method documented here: API - Save.

The Result

The record updates in any lists that are bound to the store and appears to be saved, however when I reload the webpage the old values are displayed. So the record is updated, the store see's the new data, the lists bound to the store refresh their display, but the actual representation of the data in localstorage is never updated. If you use the browser console to find and inspect the item with something like var record = summaryStore.getAt(4), you will find that the dirty attribute is false even if the save() call has not been made. Also summaryStore.getUpdatedRecords() returns an empty set regardless of whether the save() method has been called.

Further documentation

API - Models

API - Stores

API - Proxies

The Code

Store

var summaryStore = new Ext.data.Store({
model: 'EventSummary',
storeId: 'summaryStore',
sortOnLoad: true,
sorters: [
    {
        property: 'date',
        direction: 'DESC'
    }
],
proxy: {
    type: 'localstorage',
    id  : 'summary-cache'
}
})

Model

Ext.regModel('EventSummary', {
fields: [
    {name: 'child', type: 'string'},
    {name: 'date', type: 'date'},
    {name: 'description', type: 'string'},
    {name: 'event_id', type: 'int'}
],
proxy: summaryStore.proxy
})

Example

var record = summaryStore.findExact('event_id', 90)
record.date = new Date();
record.description = 'Vitamin K Shot`
record.save()

Both before and after the record.save(), the dirty flag is false. summaryStore.getUpdatedRecords() returns an empty set both before and after the save. All lists to summary store reflect the change made regardless of whether the record is saved.

Additional Notes

The store is populated with this call, which follows the declaration of the store and the model. summaryStore.load()

Upvotes: 1

Views: 1903

Answers (3)

Trevor Cox
Trevor Cox

Reputation: 11

The setDirty workaround mentioned above didn't work for me, but I found another workaround for saving records to a WebLocalStorage proxy:

record.set('field1', 'abc');
store.proxy.setRecord(record);

I'm using 1.1.1. (And btw for some reason I can't call save(); the exception says I need a url, which is inapplicable to weblocalstorage.)

Upvotes: 1

wmarbut
wmarbut

Reputation: 4685

Update

The issue can be avoided altogether by properly instantiating your model instances before passing them to the store's add method. I was just passing in JSON, which the store accepted, but apparently was causing issues.

End Update

The issue can be fixed by making changes in WebStorageProxy.js

Be advised that changing this file does not modify your sencha-touch.js file. You will have to rebuild it or modify it directly

This code was not working

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
    }

This line in particular id = record.getId(); for some reason was coming up undefined on objects that had already been added to the store. The solution is to add

if (id === undefined) {
    id = record.internalId;
}

The final code looks like

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
        if (id === undefined) {
            id = record.internalId;
        }
    }

Upvotes: 0

Anthony
Anthony

Reputation: 1714

Use the sync() method on the store to persist the data to localstorage.

Try this code instead of your example above:

var record = summaryStore.findExact('event_id', 90)
record.set('date', new Date());
record.set('description', 'Vitamin K Shot`);
record.save();

summaryStore.sync();

Upvotes: 0

Related Questions