0

I'm using Backbone.js

I have multiple lists of models on my page. Should I just make one "class" with Backbone.Collection.extend and my lists should be instances of this "class", or should I make as many classes as I have model lists on my page?

So, should I do this:

var MyCol1 = new Backbone.Collection.extend({})
var MyCol2 = new Backbone.Collection.extend({})

var listView1 = new ItemsView({ collection: new MyCol1 })
var listView2 = new ItemsView({ collection: new MyCol2 })

or rather this?

var MyCol = Backbone.Collection.extend({})
var listView1 = new ItemsView({ collection: new MyCol })
var listView2 = new ItemsView({ collection: new MyCol })

Behavior of all lists will be pretty much the same at this point but they will have different backend urls they'll be attached to.

Kamil Szot
  • 17,436
  • 6
  • 62
  • 65

1 Answers1

1

If something is differente you will find you need two different classes, or you'll find your self in a hell of conditional sentences.

You can use a custom BaseCollection with the common behavior and inherit from it for the detailed behavior. Check things like:

Community
  • 1
  • 1
fguillen
  • 36,125
  • 23
  • 149
  • 210