Sencha touch2,本地商店中的数据没有进入localstorage

时间:2013-07-25 11:54:00

标签: javascript extjs sencha-touch-2

型号:

Ext.define('SkSe.model.PlacesLocal',{
    extend:'Ext.data.Model',
    config:{
        fields:['id', 'name','icon','required_stamps', 'active_stamps','description', 'campaign_id', 'user_favorites' , 'live_action_number'],
         proxy: {
        type: 'localstorage',
        id  : 'local-places-id'

    }

    }

});

商店:

Ext.define('SkSe.store.PlacesLocal', {
    extend:'Ext.data.Store',
    config: {
        storeId: 'PlacesLocal',
        model: "SkSe.model.PlacesLocal",
         sorters: 'name',
        grouper: {
            groupFn: function (item) {
                return item.get('name')[0];
            }
        }, 
        groupDir: 'DESC'


    }
});

离线 - 在线商店同步:

Ext.define('SkSe.store.Places',{
    extend:'Ext.data.Store',

    config:{

        autoLoad:true,
        autoSync:true, 
        model:'SkSe.model.Places',
        sorters: 'name',
        grouper: {
            groupFn: function (item) {
                return item.get('name')[0];
            }
        }, 
        groupDir: 'DESC',
        proxy:{
            type:'ajax',
            url:'http://localhost/campaigns/',
            reader:{
                type:'json',
                //name of array where the results are stored
                rootProperty:'results'
            }
        },

        listeners: {
                load: function() {

                    var PlacesLocal = Ext.data.StoreManager.lookup('PlacesLocal');
                    // Clear proxy from offline store

                      if (navigator.onLine) {
                   console.log("Hm");

                    // Loop through records and fill the offline store
                    this.each(function(record) {

                        PlacesLocal.add(record.data);

                    });

                    // Sync the offline store
                    PlacesLocal.sync();
                      }
                }

    }
    }
});

显然,placesLocalstore会获取数据,但由于某种原因,它不会存储在localstorage中。

关键local-places-id出现在localstorage中,但没有任何数据。

2 个答案:

答案 0 :(得分:3)

我认为问题是当你这样做时:

PlacesLocal.add(record.data);

id字段的值为record.data。因此,记录不会被视为新记录。它也不会被认为是修改过的,因为它没有被修改,也不会被认为是删除的(解释留给了读者)。

换句话说,对于商店来说,没有什么可以同步的。任务完成。

以下是getNewRecords方法中使用的sync代码:

function() {
    return this.data.filterBy(function(item) {
        // only want phantom records that are valid
        return item.phantom === true && item.isValid();
    }).items;
}

我想你现在已经猜到了你需要做什么,但是让我感到骄傲:

var recordsData = store.getRange().map(function(record) { return record.data }),
    newRecords = PlacesLocal.add(recordsData);

Ext.each(newRecords, function(record) {
    record.phantom = true;
});

// Now it should have plenty of work!
PlacesLocal.sync();

答案 1 :(得分:0)

实际上问题是id,所以使用复制方法可以解决问题:

// Loop through records and fill the offline store
this.each(function (item, index, length){
    var newItem = item.copy()
    PlacesLocal.add(newItem);

});