2011-07-17 55 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(); 
相关问题