2012-02-17 11 views
2

私は煎茶タッチとのlocalStorageでいくつかの問題を抱えている...煎茶タッチ - のlocalStorage問題

1)私はのlocalStorageにデータを保存することができるよ、と私はページを更新するまで、それは素晴らしい作品。その後も、データにアクセスしたり、変更を加えたり、新しいエントリを追加することができます。しかし、ページがリフレッシュされるとすぐに、私の更新はすべて消えてしまいます。新しく追加されたものはすべて問題ありませんが、元のエントリへの更新は保存されません。基本的に、私はエントリを作成し、私がページをリフレッシュするまで、それをすべて更新することができます。その時点で、再度localstorageに保存されません。新しいエントリーはいつでもうまく保存されます。

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)ですか?行う?あなたのモデルが何らかの理由で編集されたことを知らないように聞こえます。 –

答えて

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()は実際に変更を保存します。

これは、特にローカルストレージを使用してデバッグするのは難しいです。次のストアについては

:あなたは次の操作を行うように誘惑されるかもしれないの最初のレコードのスペルミスを修正するには

{'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' 
                  ] 
                 } 
] 
} 

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' 
     } 
    } 
} 
}); 

のようなもので店を埋めyou'veを想定すると、

{//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(); 
} 
} 

これを試した場合、コンソールにエラーは発生しません。したがって、すべてi大丈夫です。右?

間違っている!再起動しても変更は維持されません。どうして?

レコードが「クリーン」であるため。

どうすればそれらを「汚い」ことができますか?

私が見つけた唯一の方法は、(私はもっとたくさんあると確信しているが)レコードを更新するモデルからメソッド.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();をリセットしようとしています。

関連する問題