Refetching a collection and forcing results to update values of models

Not sure if I'm missing something but I can't get this to work.
Basically, I am fetching a collection from Web service.
Then, I'd like to refresh the results every 10 minutes.
I'm doing a collectionName.fetch({parse:true, find:true, merge:true, modelOptions:{parse:true}});
But although it retrieves the new resultset, it does not update the values of existing models.
I have to do a collectionName.empty({destroy:true}) and then collectionName.fetch() to get it to work - but then of course this affects other collections and views that contain the models.
Any thoughts as to what I'm doing wrong?
Cage

Comments

  • Looking at the code, merge has it call model.set() with the new values.

    Have you tried setting a breakpoint on your model's or enyo.Models set, and see what values it is updating?

    I do have this working in my current project, but i am also using the purge flag.
  • It appears that it is not even parsing the models. I tried purge as well with same result :(
  • In looking at the source code, it seems that it only parses models on construction or if fetched via model directly. If fetched via collection, the collection parse method is run, but if the model already was constructed, it does not parse again...
  • edited December 2015
    I added one line and changed one line in the section of code from line 486 to 532) in Collection.js to make it work correctly...

    Was this a bug?
    if (found) {
    
    					// we need to ensure we've resolved the model (if necessary)
    					found = loc.resolve(id);
    
    					if (merge) {
    						attrs || (attrs = model.attributes);
    //---------Added this line ------------------------------------------//
    						modelOpts&&modelOpts.parse && (attrs = found.parse(attrs));
    						found.set(attrs, opts);
    					}
    					// with the purge flag we endeavor on the expensive track of removing
    					// those models currently in the collection that aren't in the incoming
    					// dataset and aren't being created
    					if (purge) {
    						keep || (keep = {length: 0});
    						keep[found.euid] = model;
    						keep.length++;
    					}
    				} else if (attrs && find && (found = this.store.resolve(ctor, id))) {
    					// in this case we were asked to search our store for an existing record
    					// and we found one but we didn't previously have it so we are technically
    					// adding it
    					// @NOTE: Setting the _find_ option always assumes _merge_
    					attrs || (attrs = model.attributes);
    
    //--------Changed this line from parse&&(attrs=found.parse(attrs)); ------//
    					modelOpts&&modelOpts.parse && (attrs = found.parse(attrs));
    					added || (added = []);
    					added.push(found);
    					this.prepareModel(found, opts);
    					merge && found.set(attrs, opts);
    				} else if (!attrs) {
    					added || (added = []);
    					added.push(model);
    					this.prepareModel(model);
    				} else if (create) {
    					model = this.prepareModel(attrs || model, modelOpts);
    					added || (added = []);
    					added.push(model);
    					
    					// with the purge flag we endeavor on the expensive track of removing
    					// those models currently in the collection that aren't in the incoming
    					// dataset and aren't being created
    					if (purge) {
    						keep || (keep = {length: 0});
    						keep[model.euid] = model;
    						keep.length++;
    					}
    				}
    
  • I dunno if it would be a bug, but I did make a collection parse function in my project, but mainly because my api returns an array vs an object if it comes from a collection.
  • So you are manually parsing each model in the array via the collection parse function?
  • No, i parse the response from the server, which returns an object that has a nested array in it. From the collection.parse function, i return the array of objects I got request. That array gets passed to collection.add(), and that function handles all updating all the models.
  • Okay. Just to confirm: so the second and third time you run it, it updates the models with new values? Or are you just talking about the first time it gets executed (i.e., it parses and creates the models the first time retrieved from server)?

    For me, the first time is fine but the subsequent times do not update the models with new values with the code I inserted into Collection.js
  • So, i do have it updating on multiple calls, but i do not have it setup for each model to parse on a collection fetch. The collection parse method parses out what is needed for each model.
Sign In or Register to comment.