2010-11-06 53 views
8

我有一個JsonStore具有以下字段:檢查中儲存,以避免存在類似的記錄複製

id 
date 
time 
type 

我有收集三個字段的表單(datetimetype),並插入一個新的記錄進入商店(我分別保存商店)。我想執行一次檢查,看存儲中是否存在具有相同字段值組合的記錄,以避免重複輸入。

我設法在另一家商店這樣的檢查重複的ID:

find = DepartmentMemberStore.find('member_id', value_from_form); 
if (find != -1) { 
    // popup error window 
    return; 
} else { 
    // add new record to store 
} 

我不知道如何檢查一家商店,看看是否多個域值匹配。

回答

16

我已經在這種情況下使用了Store的findBy(Function fn, [Object scope], [Number startIndex])。對商店中的每個記錄都調用fn函數,並將當前記錄及其相應的ID傳遞到函數中。因此,您可以使用當前記錄的字段與每個表單字段進行比較。

這裏是您的具體情況爲例:

var recordIndex = DepartmentMemberStore.findBy(
    function(record, id){ 
     if(record.get('date') === date_from_form && 
      record.get('time') === time_from_form && 
      record.get('type') === type_from_form){ 
       return true; // a record with this data exists 
     } 
     return false; // there is no record in the store with this data 
    } 
); 

if(recordIndex != -1){ 
    alert("We have a duplicate, abort!"); 
} 
+1

正是我需要的。謝謝! – 2010-11-07 14:36:44