Sencha Touch -重新加载/刷新应用程序后清空商店



我正在努力寻找如何将数据保存到本地存储的方法,但现在我这边似乎没有运气。我可以成功地添加/删除项目在商店,然而,每当我尝试刷新或重新加载页面(谷歌chrome)看起来像没有数据在商店中找到。

下面是我的代码:

模型:Pendingmodel.js

Ext.define('mysample.model.PendingModel', {
extend: 'Ext.data.Model',
config: {
    fields: [
        {
            name: 'id',
            type: 'int'
        },
        {
            name: 'ALIAS'
        },
        {
            name: 'OBJECT'
        },
        {
            name: 'DATETIME'
        },
        {
            name: 'FIELDVALUE'
        }
    ],
    proxy: {
        type: 'localstorage',
        id: 'PendingModelProxy'
    }
}

});

存储:Pendingstore.js

Ext.define('mysample.store.PendingStore', {
extend: 'Ext.data.Store',
requires: [
    'mysample.model.PendingModel'
],
config: {
    autoLoad: true,
    model: 'mysample.model.PendingModel',
    storeId: 'PendingStore'
}

});

控制器:

onDraftCommand: function (form, isTapped) {
    var isValid = true;
    var me = this, getForm = me.getLeadsView();
    var pendingItems = getForm.getValues();
    var cleanItems = getForm.getValues();
    cleanItems['reset'] = function() {
        for (var prop in this) {
            delete this['reset']; 
            delete this['eventParameter']; 
            this[prop] = '';
        }
    }
    if(isTapped == true && isValid == true) {
        Ext.Msg.confirm('Message', 'Are you sure you want to save this on draft?', function (btn) {
            switch (btn) {
                case 'yes':
                    var date = new Date(), id = date.getUTCMilliseconds();
                    var obj = {
                        'ALIAS': 'leadsview',
                        'OBJECT': 'Leads Form',
                        'id': id,
                        'DATETIME': date,
                        'FIELDVALUE': pendingItems,
                    };
                    console.log('Leads pending store');
                    console.log(obj);
                    Ext.data.StoreManager.lookup('PendingStore').add(obj);
                    Ext.data.StoreManager.lookup('PendingStore').sync();
                    console.log(Ext.data.StoreManager.lookup('PendingStore'));
                    //clear form
                    cleanItems.reset();
                    getForm.setValues(cleanItems);
                    //Ext.getCmp('signatureField').removeImage();
                    break;
                default:
                    break;
            }
        });
    }
}

在模型中添加标识符策略,如下所示。

Ext.define('mysample.model.PendingModel', {
extend: 'Ext.data.Model',
config: {
    identifier: 'uuid', // add this
    fields: [
        {
            name: 'id',
            type: 'int'
        }
    ],
    proxy: {
        type: 'localstorage',
        id: 'PendingModelProxy'
    }
}

您需要在模型中添加唯一标识符,如下所示:-

Ext.define('mysample.model.PendingModel', {
    extend: 'Ext.data.Model',
    requires: [
    'Ext.data.identifier.Uuid'
    ],
    config: {
        identifier: {
            type: 'uuid',
            isUnique: true
        },
    fields: [
    {
        name: 'id',
        type: 'int'
    },
    {
        name: 'ALIAS'
    },
    {
        name: 'OBJECT'
    },
    {
        name: 'DATETIME'
    },
    {
        name: 'FIELDVALUE'
    }
],
proxy: {
    type: 'localstorage',
    id: 'PendingModelProxy'
}
     }
});

最新更新