Duplicated records when fetching Collection

Me again :).

I'm having big troubles with Collections, when i try to retrieve Models I already fetched, and stored locally.
A previous thread was dealing with this issue for Models, with the simple solution of making a findOrFetch method, which creates Model from Store if it exists.

But for Collection, it's more complicated, since some could exist, and some may be fetched, but from a single url.
Then, i would like to know the best practice to have a findOrCreate from a Collection.
In fact, the ideal behavior would be to fetch all records from url, to find their local version, and then, update or create those local records.
Moreover, i'm planning to retrieve partial Models from Collections, and full versions in Model.fetch...

Persistency is very hard to understand in Enyo 2.3. I may not the only one to have those issues.
I don't really want to rewrite all the Collections and Model fetching methods...

Thank you for your great help.


  • edited February 2014
    At this moment, I found a simple solution by implementing a modified version of Collection.createRecord.
    If first make a findLocal to the store, and I should add setObject to update local version.

    So here is my Own collection createRecord:
        createRecord: function (attrs, props, i) {
            var defaults = {owner: this},
            // we have to check to see if we marked these attributes as being fetched
            // by their isNew flag and propagate that properly if so
            if (attrs && attrs.isNew === false) {
                (props || defaults).isNew = false;
                // remove the flag so that it doesn't show up as an attribute of the
                // the record
                delete attrs.isNew;
            // find Local version
            rec = enyo.store.findLocal(this.model,{id: attrs.id})
            // Add setObject here to update local version
            if (rec ==  undefined)
                // Create Record from fetched entry
                rec = this.store.createRecord(this.model, attrs, (props? enyo.mixin(defaults, props): defaults));
            // normalize the index we're adding this record at knowing that a false
            // indicates we don't insert the record (because it probably already is) and
            // we don't update the entry here because it will be handled in the caller
            // if that is the case
            i = (false === i? -1: (i !== null && i >= 0? i: this.length));
            rec.addListener("change",  this._recordChanged);
            rec.addListener("destroy", this._recordDestroyed);
            if (i >= 0) { this.add(rec, i); }
            return rec;
    It works pretty well.

    I'm working on adding find GET parameters, to fetch only filtered items according to my REST API, so that I will have find_by_attributes methods for collections and models.

    NB: this code is not commitable, i should have used pk instead of id, but I needed a quick fix.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!