Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if similar record exists in store to avoid duplicates

Tags:

store

extjs

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 would 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.

like image 582
Jure Srpcic Avatar asked Nov 06 '10 21:11

Jure Srpcic


1 Answers

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!");
}
like image 195
McStretch Avatar answered Sep 19 '22 07:09

McStretch