The proper use of a Collection, Model, and Source

EnyoJs Developers,

I am in need of a little help and direction around how I should be leveraging a collection, model, and source in my application. I have provided a link below to my jsfiddle showing a basic example of my application. I am using Google Sheets as my backend data source. I followed the Moonstone App tutorial to help build a starting point for my app. I created a collection, model, and source that can be seen in my jsfiddle. When you first open the application it seems to do what I need which is to pull in the jsonp feed. It takes in the information and uses binding in my app to display the content. All looks good that is untill I tap to refresh the data or the 20 second interval passes and it refreshes the data. What happens is the application will go out grab the data and append it to my list. The issue is most of the data is the same. I really need it to merge the same data and only append the new data. Is this even possible in my current design? I have seen people use ajax calls outside of a collection, then take the inresponse data and fill the collection, is that the proper procedures? I have done a test and if I change the ( .fetch() ) to ( .fetchAndReplace() ), I kind of get what i want. It removes everything and adds all the received content. Should I be using this method?
I am looking for a process where the old content would not really refresh (reload on the screen), just the newly added content.
I know there were several questions I have but any help, suggestion, or advice would be greatly appreciated.
Thank in advance for all your help.

jsfiddle.net/fred_d_castillo/dU2kF/3/


Freddy

Comments

  • I think instead of a fetch and replace you can extend the add method of collection and check whether the record already exists so that you don't need to add it to the collection and thereby cause a re-render for it. You should basically be doing a diff.
    Here is a diff collection that i use for my app (its written in coffeescript)

    enyo.kind
    name: 'DiffCollection'
    kind: 'enyo.Collection'
    /*
    * This method is overriden to improve the performance of collections and
    * DataReapeaters if the item is already in the collection then it doesn't
    * add it to the collection thereby preventing a re-render of the already
    * present items
    * If the item has changed some parameter then that item is removed and
    * the the new item is added
    * @param {JSON} json [description]
    * @param {index} i [description]
    */
    add: (json, i) ->
    arr = []
    _.forEach @records, (record) ->
    arr.push JSON.parse(record.toJSON())
    if _.where(arr, json).length is 0
    Logger.log 'Add'
    index = _.findIndex arr, {'id': json.id}
    if index > -1
    Logger.log index # check if value has cahnged if not remove
    @at(index).set 'value', json.value
    else
    @inherited arguments
  • edited July 2014
    Hi Freddy,

    Have you tried specifying strategy:"merge" in your fetch options? There is some info on this in the 2.4.0 documentation: https://github.com/enyojs/enyo/blob/2.4.0/source/data/Collection.js#L76. Hope this helps! Let us know if you run into any trouble.
  • Pyros2097 / arrontam,

    Thanks for your information. I started looking the ( strategy:"merge" ) direction for now since I do not know coffeescript. I used merge in my fetch option but I am still getting the same results. I have done a little testing an I know it is executing the merge function but my guess is that it is not seeing the results as the same so instead of merging the data it adds it. Anyone know what could be wrong with my data format?
    Thanks


    Freddy
  • I think merge checks the property that is stated in primaryKey. The default is "id", maybe try and set that to a field that the web service returns
  • So I have added the primaryKey property to my Model. At this time I do not know how it should all work but this is what I am getting after adding the primaryKey property.
    Uncaught enyo.Store.addRecord: duplicate record added to store for kind `google.DriveModel` with primaryKey set to `username` and the same value of `thisguy` which cannot coexist for the kind without the `ignoreDuplicates` flag of the store set to `true`
    I tried to to test adding "ignoreDuplicates" flag but I am not understanding where it should go. I added it in the section for my source but it doesn't look to be working either.
    Any additional information that can be provided would be greatly appreciated.

    Here is the link to my latest jsfiddle
    jsfiddle.net/fred_d_castillo/dU2kF/6/
    Thanks


    Freddy
  • Can anyone tell me where I am supposed to specify the "ignoreDuplicates: true" property? I have not quite figured out where it should go and I keep getting the duplicate recode warning and will not let me move forward.
    Thanks


    Freddy
  • Hi Freddy,

    You can set enyo.store.ignoreDuplicates = true; somewhere in your general code, i.e. http://jsfiddle.net/aarontam/dU2kF/7/
Sign In or Register to comment.