TL DR
Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype)
"correct" way to inherit Backbone View inherited from another "class"?
Long read
We are redesigning our site using Backbone and are working to enable some display features.
I have a Backbone view that handles placing a <div>
at specific points in a browser window; this seems like a natural continuation for the Google Map API to place them in geographic coordinates.
According to the Google API , in order to create a custom overlay, you create a new object and set the prototype for this object in a new instance of google.maps.OverlayView. Then you implement three functions on top of this object so that the object responds to:
onAdd
draw
onRemove
Where onAdd
is responsible for creating the HTML and then applying it on top of the Map. Then it calls draw
, which correctly positions the element according to the LatLng pairs and constraints that you provided. onRemove
is called when you want to get rid of your layer.
So, I changed my view to include these three methods (which are simply called render and unrender and are tied to my collection). And then, to do the “magic”, I do:
PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype)
Does this look right? I can publish the code for the View and Model on which it is based, but to be honest, they are not related to this example - the code works, and I can place a custom div
generated using the Backbone model, view and the component controller on the map without of the problems I'm asking about, I think (and maybe this question is more suitable for .se programmers, so let me know and I will translate it).
This is probably the easiest way to make my PinView both Backbone View and OverlayView Google Maps, but I'm not 100% comfortable with prototype inheritance to know that I'm doing something “wrong” or something broke on the way .