开发者

Check if similar record exists in store to avoid duplicates

I have a JsonStore with the following fields:

id
date
time
type

I have a form that collects the three fields (date, time, type) and inserts a new record into the store (I save the store separately). I wo开发者_开发知识库uld like to perform a check if a record with the same combination of field values already exists in the store to avoid duplicate entries.

I have managed to check for duplicate ID's in another store like this:

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

I don't know how to check a store to see if multiple field values match.


I've used Store's findBy( Function fn, [Object scope], [Number startIndex] ) for this situation. The function fn is called for every record in the store, and the current record and its corresponding id are passed into the function. Thus you can use the current record's fields to compare against each form field.

Here's an example for your situation:

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!");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜