2012-02-17 71 views
2

我在与煎茶触摸和localStorage的几个问题...煎茶触摸 - localStorage的问题

1)我能数据保存到本地存储,直到我刷新页面,它的伟大工程。在那之后,我仍然可以访问数据,看起来可以做出更改,甚至可以添加新条目。但只要刷新页面,我的所有更新都将消失。任何新添加的内容都可以,但是原始条目的更新不会被保存(希望这是有道理的)。基本上,我可以创建一个条目并更新它,直到我刷新页面。此时它不会再保存到本地存储。新的条目总是可以很好地保存。 2)当我删除所有条目,然后刷新页面时,我在控制台中得到这个错误:“未捕获的TypeError:无法读取'null'属性'id'。它看起来像试图加载一些东西,即使没有东西。我不知道为什么它会在这种情况下抛出一个错误,而不是当页面最初被加载时,因为它应该是同样的事情吗?

型号:

Ext.regModel('inventoryItem', { 
     idProperty: 'id', 
     fields: [ 
      { name: 'id', type: 'int' }, 
      { name: 'date', type: 'date', dateFormat: 'c' }, 
      { name: 'title', type: 'string' }, 
      { name: 'quantity', type: 'int' }, 
      { name: 'size', type: 'int' }, 
      { name: 'cost', type: 'double' }, 
      { name: 'startDate', type: 'date' }, 
      { name: 'endDate', type: 'date' }, 
      { name: 'dailyAvgCost', type: 'string' }, 
      { name: 'dailyAvgSize', type: 'string' } 
     ] 
    }); 

商店:

Ext.regStore('inventoryItemsStore', { 
     model: 'inventoryItem', 
     sorters: [{ 
      property: 'id', 
      direction: 'DESC' 
     }], 
     proxy: { 
      type: 'localstorage', 
      id: 'inventory-app-store' 
     }, 
     getGroupString: function (record) { 
      return ''; 
     } 
    }); 

处理程序:

 { 
      var currentinventoryItem = CostsApp.views.inventoryEditor.getRecord(); 
      CostsApp.views.inventoryEditor.updateRecord(currentinventoryItem); 
      var inventoryStore = CostsApp.views.inventoryList.getStore(); 
      if (null == inventoryStore.findRecord('id', currentinventoryItem.data.id)) 
      { 
        inventoryStore.add(currentinventoryItem); 
      } 

      inventoryStore.sync(); 
      inventoryStore.sort([{ property: 'date', direction: 'DESC'}]); 
      CostsApp.views.inventoryList.refresh(); 
     } 

任何人都可以指出someth很明显,我做错了?

+0

CostsApp.views.inventoryEditor.updateRecord(currentinventoryItem);做?这听起来像你的模型不知道他们已经出于某种原因被编辑。 – 2012-02-17 06:30:21

回答

0

看起来一切都很好 - store.add()方法与store.sync()一起使用。检查如何将数据加载到列表中的方式,可能是刚加载的数据。至于我,商店的autoload财产总是不工作(可能是有一些原因,我不否认),而我只是使用store.load()来代替。此外,您可以直接检查localStorage以确定此确定(即代理ID项localStorage.getItem('inventory-app-store')应该给你类似"1,2,3" - 成功持久记录的ID)。

这里是工作的例子,以防万一。

new Ext.Application({ 
    name: 'MyApp', 
    launch: function() { 
     this.viewport = new Ext.Panel({ 
      fullscreen: true, 
      id : 'mainPanel', 
      layout: 'card', 
      dockedItems: [{ 
       xtype: 'toolbar', 
       dock : 'top', 
       items: [{ 
        xtype: 'textfield', 
        flex: 1 
       }, { 
        text: 'Add', 
        listeners: { 
         tap: function() { 
          var view = this.viewport, 
           list = view.items.first(), 
           store = list.store, 
           fld = view.dockedItems.first().items.first(), 
           val = fld.getValue(); 
          store.add({item: val}); 
          store.sync(); 
          fld.reset(); 
         }, 
         scope: this 
        } 
       }] 
      }], 
      items : [{ 
       xtype: 'list', 
       itemTpl : '{item}', 
       store: new Ext.data.Store({ 
        fields: ['item'], 
        proxy: { 
         id: 'items', 
         type: 'localstorage', 
        } 
       }).load() 
      }] 
     }); 
    } 
}); 
0

如何在if-condition之前调用sync()方法。它为我工作。

var inventoryStore = CostsApp.views.inventoryList.getStore(); 
inventoryStore.load(); 
inventoryStore.sync(); 
1

存储同步()对我来说已经证明非常棘手,这可能有助于阐明一些原因。

.sync()将不会更新远程或本地存储记录,除非它们很脏。所以他们不会执着。

使用JSON类型的读者可以很容易地使直接修改记录的错误,必须使用模型的方法来做到这一点,特别是.set(),因为集()定义记录为“脏”,然后才sync()实际上将保存更改。

这特别难以使用本地存储进行调试。

对于下列店:

Ext.define('MyApp.store.LocalStuff',{ 
extend: 'Ext.data.Store', 
xtype:'localstuff', 

config: { 
    fields:['stuffId','stuffName','stuffArray'], 
    storeId:'LocalStuffId', 
    autoLoad:false, 
    proxy:{ 
     type:'localstorage', 
     id:'idForLocalStorage', 
     reader: { 
      type: 'json', 
      rootProperty: 'stuffArray' 
     } 
    } 
} 
}); 

假设......你填充店里的东西,如:

{'stuffArray':[ 
{'stuffId':'130', 'stuffName':'floob', 'stuffArray':[ 
                  'first', 
                  'second', 
                  'tird' //here be a typo (on purpose) 
                  ] 
                 }, 
{'stuffId':'131', 'stuffName':'sateo', 'stuffArray':[ 
                  'primero', 
                  'segundo', 
                  'tercero' 
                  ] 
                 }, 
{'stuffId':'133', 'stuffName':'tolus', 'stuffArray':[ 
                  'prima', 
                  'secunda', 
                  'tertia' 
                  ] 
                 } 
] 
} 

要解决的第一个创纪录的拼写错误,你可能会尝试做到以下几点:

{//in a given controller or view 
someEventHandler:function(){ 
    var stor = Ext.getStore('LocalStuffId'); 
    var firstRecord = stor.getAt(0);//which will get you a record in the form of a Model 
    firstRecord.get('stuffArray')[2]='third';//that will fix it 
    //now we just sync() to make the change persist when we close the app 
    stor.sync(); 
} 
} 

如果您尝试过,控制台上将不会出现错误,所以,所有我好的。对?

错!更改将不会在重新启动时持续。为什么?

因为记录是“干净的”。

我们如何“肮脏”起来?

我发现的唯一方法(我敢肯定有很多更多)是使用方法.SET()从模型更新记录,那会告诉店里脏了

这听起来很奇怪,我的MVC未经训练的大脑,更好地解决了上面的例子。

{//in a given controller or view 
someEventHandler:function(){ 
    var stor = Ext.getStore('LocalStuffId'); 
    var firstRecord = stor.getAt(0);//which will get you a record in the form of a Model 
    //you might go around the following 2 lines in many ways, the important part is .set() after that 
    var theArrayToFix = firstRecord.get('stuffArray'); 
    theArrayToFix[2]='third'; 

    firstRecord.set('stuffArray',theArrayToFix);//set() dirties the record 
    //NOW we sync() to make the change persist when we close the app 
    stor.sync(); 
} 
} 

所有这一切都可能是有人从煎茶触摸1来明显的,但对于像我这样的新手,它是接近一天的工作要弄清楚。

希望这可以帮助别人,它将在未来可以帮助我。

1

它可能是别的东西。

您正在使用“ID”作为示范田键名,更改到别的东西,如“inventory_id”,然后再试一次。

我的印象是id由localstorage API在内部使用,所以是保留的。

3

我有同样的问题,我将我的代理移入模型,这解决了我的问题。您的代码修改应该是这样的:

型号:

Ext.regModel('inventoryItem', { 
    idProperty: 'id', 
    fields: [ 
     { name: 'id', type: 'int' }, 
     { name: 'date', type: 'date', dateFormat: 'c' }, 
     { name: 'title', type: 'string' }, 
     { name: 'quantity', type: 'int' }, 
     { name: 'size', type: 'int' }, 
     { name: 'cost', type: 'double' }, 
     { name: 'startDate', type: 'date' }, 
     { name: 'endDate', type: 'date' }, 
     { name: 'dailyAvgCost', type: 'string' }, 
     { name: 'dailyAvgSize', type: 'string' } 
    ], 
proxy: new Ext.data.LocalStorageProxy({ 
    id: 'inventory-app-store' 
}) 
}); 

商店:

Ext.regStore('inventoryItemsStore', { 
    model: 'inventoryItem', 
    sorters: [{ 
     property: 'id', 
     direction: 'DESC' 
    }], 
    getGroupString: function (record) { 
     return ''; 
    } 
}); 
1

我知道这老,但也许它会帮助别人。 我有同样的问题,并尝试重置我的本地存储window.localStorage.clear();,它的工作原理。