0

My collection:

return Backbone.Collection.extend({

    url: config.API + 'Events?count=1',

    initialize: function() {
        this.fetch();
    },

    more: function() {
        this.fetch({ remove: false, merge: true });
    }

});

Every request to the server returns this:

[{"EventId":1,"User":{"UserId":6,"UserName":"phil","Photo":null},"Latitude":"46.3165906656757","Longitude":"46.5820399671793","Description":"Hdbdjcjfgkgkgjf","EventDate":"2014-05-10T21:44:36","DateCreate":"2014-11-28T19:19:51","LastComments":[{"CommentId":1,"User":null,"Text":"HELLO","DateCreate":"2014-11-28T17:41:46","Likes":null,"LikesCount":0},{"CommentId":2,"User":null,"Text":"HELLO","DateCreate":"2014-11-29T00:59:43","Likes":null,"LikesCount":0},{"CommentId":3,"User":null,"Text":"HELLO","DateCreate":"2014-11-29T01:07:13","Likes":null,"LikesCount":0},{"CommentId":4,"User":null,"Text":"HELLO","DateCreate":"2014-11-29T01:10:41","Likes":null,"LikesCount":0},{"CommentId":5,"User":null,"Text":"HELLO","DateCreate":"2014-11-29T01:17:33","Likes":null,"LikesCount":0},{"CommentId":6,"User":null,"Text":"HELLO","DateCreate":"2014-11-29T01:29:59","Likes":null,"LikesCount":0}],"Photos":null,"LastLikes":null}]

So, after initialization we have only one model in the collection. But every time I call more() this model doesn't update and after all we have so many duplicates in the collection as many times I called more(). This behavior seems strange to me, since in the documentation I see:

The set method performs a "smart" update of the collection with the passed list of models. If a model in the list isn't yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren't present in the list, they'll be removed. All of the appropriate "add", "remove", and "change" events are fired as this happens. Returns the touched models in the collection. If you'd like to customize the behavior, you can disable it with options: {add: false}, {remove: false}, or {merge: false}.

alexb
  • 880
  • 11
  • 16

1 Answers1

1

The problem is you don't have a traditional id field. See Backbone - using a different field name for id

You need to specify a custom model type.

var EventModel = Backbone.Model.extend({
   idAttribute: "EventId"
});

Backbone.Collection.extend({
   model: EventModel
})
Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • @Sandrik good to hear, but do spend an afternoon reading thru the code, you'll be more productive http://backbonejs.org/docs/backbone.html – AJcodez Dec 14 '14 at 08:40