Can a Backbone.js View have a Model and a Collection?
Asked Answered
C

1

5

Can a Backbone.js View have a Model and a Collection?

The reason I am asking is because I'm working with a View that holds a map with places plotted on it (using the leaflet library).

The Collection is of a group of models that hold coordinates to be plotted on the map.

The Model holds stuff like the map bounds and map shape.

Not sure how else to pull this one off...

Any help is appreciated, thanks!

Computation answered 23/7, 2012 at 22:3 Comment(0)
R
7

Yes, you can pass it in as simply as:

new view({model: m, collection: c});

and inside the view you can access both with:

this.model
this.collection

jsfiddle for example

Receive answered 23/7, 2012 at 22:18 Comment(2)
I also find useful (for readability and ease of support) to rename model and collection to something more meaningful. E. g. new MapView({map: mapModel, markers: markersCollection}). If you opt to do this, don't forget to access them via this.options.map or add this.map = this.options.map; this.markers = this.options.markers to your map view's initialize method.Dinar
jsfiddle now gives a 404Ecstatic

© 2022 - 2024 — McMap. All rights reserved.