2011-07-17 57 views
1

我正在Sencha Touch中開發移動應用程序,並且需要在本地存儲以前搜索的詳細信息而不會有重複。Sencha Touch商店中的唯一值

搜索,存儲和顯示工作正常,但是如果我做了兩次相同的搜索,則條目會在我的商店中重複。

我預計商店的「ID」會這樣做,但有些東西似乎是錯誤的。

這是商店的代碼。

Ext.regModel('sSaved', { 
     fields: [ 
      {name: 'IDs',  type: 'int'}, 
      {name: 'Outward',  type: 'string'}, 
      {name: 'Return',  type: 'string'}, 
      {name: 'Pickup',  type: 'string'}, 
      {name: 'Destination',  type: 'string'} 
     ], 
     proxy: { 
      type: 'localstorage', 
      id:'IDs' 
     } 
    });    

    var savedJobs = new Ext.DataView({ 
     store: new Ext.data.Store({ 
      model: 'sSaved', 
      storeId: 'allSaved'  
     }), 
     tpl: new Ext.XTemplate(
      '<tpl for=".">', 
       '<table class="item" style="margin:auto;">', 
        '<tr><td class="title">ID</td><td>{IDs}</td></tr>', 
        '<tr><td class="title">Outward</td><td>{Outward}</td></tr>', 
        '<tr><td class="title">Return</td><td>{Return}</td></tr>', 
        '<tr><td class="title">Pickup</td><td>{Pickup}</td></tr>', 
        '<tr><td class="title">Destination</td><td>{Destination}</td></tr>', 
       '</table>', 
      '</tpl>' 
     ), 
     itemSelector: "table.item", 
     autoHeight:true, 
     emptyText: 'No Saved Jobs', 
     autoLoad: true 
    });  

並設置商店的代碼。

var fetched = Ext.decode(result.responseText); 
    var details = fetched.apiResponse.details; 
    savedJobs.store.load(); 
    savedJobs.store.add({ 
     "IDs":details.ID, 
     "Outward":details.Outward, 
     "Return":details.Return, 
     "Pickup":details.Pickup, 
     "Destination":details.Destination 
    }); 
    savedJobs.store.sync(); 

我可以解決此通過某種方式搜索商店,並檢查是否添加前值存在,但我敢肯定有一個更簡單的方法。

如果這個search-> check-> add是需要的方法,那麼資源密集度最低的方法是什麼?

回答

1

檢查現有值不會受到傷害 - 我保證;)另外,您應該在load()完成後執行add()。由於加載是異步的(在配置中沒有async:false),您可以通過store的'load'事件指定依賴於該數據發生的任何事情,或者2)通過指定商店的回調方法負載配置:

var fetched = Ext.decode(result.responseText); 
var details = fetched.apiResponse.details; 
savedJobs.store.load({ 
    callback: function() { 
     if(!savedJobs.store.getById(details.ID) { 
      savedJobs.store.add({ 
       "IDs":details.ID, 
       "Outward":details.Outward, 
       "Return":details.Return, 
       "Pickup":details.Pickup, 
       "Destination":details.Destination 
      }); 
     } 
    } 
}); 

savedJobs.store.sync(); 
相關問題