PDA

View Full Version : Automatically overwrite records with identical idProperty content on add/load?



daubman
17 Jan 2012, 9:09 AM
Is there any way to set a model or its associated store to automatically overwrite existing records with new records when the idProperty of an incoming record collides with an existing record?

It seems there should be a way to enforce the uniqueness of a property being defined as unique...

If not, is the 'best' way to:
a1) iterate over all incoming records
a2) getById each incoming idProperty against the existing store
a3) build an array of records returned by getById where idProperty matched an existing record in store
a4) call remove, passing in the array of records to be removed built in a3
a5) add the incoming records to the store now that any overlapping records have been removed

b1) iterate over all incoming records
b2) indexOfId each incoming idProperty against the existing store
b3) if idexOfId is not -1, call removeAt, passing in the found id
b4) add the incoming records to the store now that any overlapping records have been removed

c) some other method?

Also, what is the difference between a Store's loadData and add methods (when should one be used instead of the other)?

Some background:
Data is being loaded into different "temp" stores via different Models/proxies that correspond to the source (actually, data is being loaded via websockets and the resulting JSON is parsed by the temp models). These "temp" models normalize the data, which is then added to the "central/normal" store via a process like:


tempStore.loadRawData(jsonFromWS, false);
centralStore.loadData(tempStore.getRange(), true); //<== need to de-dupe this!


It is at the point where data is going from temp store to central store where de-dupe needs to occur, as the incoming data may include updates to existing records, as well as new records.

mitchellsimoens
17 Jan 2012, 9:22 AM
When you add records, it will simply just add it to the data mixedcollection.

mvarshavsky
17 Jan 2013, 4:11 PM
I'm also looking for a sort of upsert functionality... Is there anything like that available?
The only thing I'm coming up with for daubman's (C) is:


instance = targetStore.getById(updatedRecordId)
instance.set(newModelConfig)

lrayman
1 Aug 2013, 5:41 AM
The model needs a correctly defined idProperty (by default, "id"), and the server needs to set that property correctly.

When you want to 'update' the store, call store.load({addRecords:true, ...})

The if the incoming records match (by id) any existing ones on the store, those will be replaced, and new records will be added. Any views on top of the store (in my case, a grid) are updated, too.

MY problem is - I want the store to send an event indicating which rows (and, in the best of all worlds, fields) have been updated, so I can have the grid cell blink (or change color or anything) to draw attention to the update.