How to merge records from different collections

edited November 2013 in Enyo 2.4
Hi,

I'm trying out the new MVC features of Enyo 2.3 and was wondering how to solve the following issue:

I have a model kind "Video" and my backend has various url's to retrieve collections of those (to be clear, it are json objects with meta data about video's and not video data itself :)

For example, /videos/latest provides the latest added videos and /videos/highlights provides some hand picked collection of videos. In my Enyo project I added the collections HighlightVideos and LatestVideos with their model property both set to Video and their url property set to the above mentioned url's.

When I fetch both collections, the default Enyo Store complains about duplicates. This is expected, since it's perfectly acceptable that the Highlight collection contains some videos also included by the Latest collection. I know about the ignoreDuplicates option, but I don't want to treat them as duplicates. Ideally I would like the store to behave something like: "If a record of this kind with this primary id already exists in the store, use the record from the store and discard the fetched one". I looked in all the documentation I could find, but found nothing how to handle a case like this. My first reaction was that there must be an option to provide a merge strategy for a store, like you can provide for collections, but I couldn't find anything like this.

So my question is: how to handle multiple collections with potentially 'shared' records?

Thanks,
Vincent

Comments

  • I was hoping that somebody from the Enyo team could advise how to handle the above described scenario, as this situation seems pretty common to me.

    Let me try to rephrase my question more concisely.

    If one has two collections that:

    * fetches records of the same model kind from two different urls
    * potentially contains identical records
    * should not treat identical records as duplicates, but as references to the same record

    How could or should one handle this with Enyo's MVC features?

    Best,
    Vincent
  • This is actually a pretty tough problem to try and handle it universally. There is currently no way to 'merge' records into an existing store although something like this is in the works but not for 2.3 release. Some platforms, by default, do not internally index records for you thus you can have an unknown number of instances of the same 'data' but stored in multiple records. This is ok in some cases. In your case, however, you want to reuse the existing record although you've requested a new set of data from the remote in a different collection that is backed by the same store. The problem with a universal solution to the issue is that you may have pulled a more current version of the record but do not want to merge this updated data with the existing record or, maybe you do, or maybe you have local changes that require a more granular level merge control.

    If you fetched this new data into the collection where the record instance already existed you could merge it, no issue. So, since the problem is in the new collection it would seem to me you can handle this at the collection-level and simply add a new strategy to deal with incoming data. Something like this perhaps...

    https://gist.github.com/clinuz/3389dbdd3a38dd8fc143

  • Thank you for the explanation and suggestion! I was thinking of implementing something along the lines of your gist.

    I didn't expect the Enyo store handling the merge automatically for me, as I understand their are situations where you don't want this behaviour, but I was hoping that I could provide some merge/replace strategy per model kind for a store.

    Thanks,
    Vincent

Sign In or Register to comment.