Skip to content Skip to sidebar Skip to footer

Backbone.js With Google Maps - Problems With This And Listeners

I have a module I created for Google Maps v3 that I'm trying to convert into a Backbone.js view constructor. Here's my view module so far: I'll explain the problems I'm having afte

Solution 1:

Take the anonymous functions called on dragend and bind explicitly.

_.bindAll(this, 'dragMarker', 'dragMap');
google.maps.event.addListener(this.marker, "dragend", this.dragMarker);
/* etc ... */

This way this will always be tied to CreateMap even if called out of context.

Solution 2:

I solved this problem by using the that/self hack common in Javascript.

var self = this;

google.maps.event.addListener(this.marker, "dragend", function() {
  self.latlng = this.getPosition();
  self.map.panTo(self.latlng);
});

google.maps.event.addListener(this.map, "dragend", function() {
  self.latlng = this.getCenter();
  self.marker.setPosition(self.latlng);
});

If anyone has a solution that doesn't require this hack, I'm all ears.

Post a Comment for "Backbone.js With Google Maps - Problems With This And Listeners"