Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js with Google Maps - problems with this and listeners
    primarykey
    data
    text
    <p>I have a module I created for Google Maps v3 that I'm trying to convert into a Backbone.js view constructor.</p> <p>Here's my view module so far: I'll explain the problems I'm having after the code:</p> <pre><code>pg.views.CreateMap = Backbone.View.extend({ tagName: "div", className: "map", events: {}, latitude: "-23.56432", longitude: "-46.65183", initialize: function() { _.bindAll(this, 'render', 'dragMarker', 'dragMap'); this.latlng = new google.maps.LatLng(this.latitude, this.longitude); var myOptions = { zoom: 16, center: this.latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; this.map = new google.maps.Map($(this.el)[0], myOptions); this.marker = new google.maps.Marker({ map: this.map, position: this.latlng, draggable: true }); google.maps.event.addListener(this.marker, "dragend", this.dragMarker()); google.maps.event.addListener(this.map, "dragend", this.dragMap()); }, render: function() { return this; }, dragMarker: function() { this.latlng = this.marker.getPosition(); this.map.panTo(this.latlng); }, dragMap: function() { this.latlng = this.map.getCenter(); this.marker.setPosition(this.latlng); } }); </code></pre> <p>The problem I'm having is with the Google Maps event listeners and how "this" is handled.</p> <p>I originally didn't have the dragMarker and dragMap methods and instead these two in the initialize block:</p> <pre><code>google.maps.event.addListener(this.marker, "dragend", function() { this.latlng = this.marker.getPosition(); this.map.panTo(this.latlng); }); google.maps.event.addListener(this.map, "dragend", function() { this.latlng = this.map.getCenter(); this.marker.setPosition(this.latlng); }); </code></pre> <p>The problem I encountered with this first approach is that "this" inside those anonymous functions referred to "this.marker" and "this.map" respectively. The problem with this first approach was that in the first listener, I had no way of referring to "this.map" and therefore could not perform a panTo(). With the second listener, I had no way of referring to "this.marker" and therefore could not recenter the the map around that marker using setPosition().</p> <p>I then thought that I could pull out the anonymous functions in the listeners and declare them as methods of the view, which I would then perform a _.bindAll(this, "dragMarker", "dragMap");</p> <p>The problem with this approach is that I then had to write the listeners in the event block like so:</p> <pre><code>google.maps.event.addListener(this.marker, "dragend", this.dragMarker()); google.maps.event.addListener(this.map, "dragend", this.dragMap()); </code></pre> <p>This meant that when I called the constructor with newmap = new pg.views.CreateMap; that the "this.dragMarker()" and "this.dragMap()" were evaluated immediately instead of being evaluated as a callback when the "dragend" event is triggered. </p> <p>No problem I thought and then wrapped those up in anonymous functions like so:</p> <pre><code>google.maps.event.addListener(this.marker, "dragend", function() { this.dragMarker(); }); google.maps.event.addListener(this.map, "dragend", function() { this.dragMap(); }); </code></pre> <p>Unfortunately this also brings me back to an earlier problem that the "this" in "this.dragMarker" no longer refers to the parent object I constructed, but instead refer to "this.marker" again. The same problem occurs with the second listener.</p> <p>I'm completely stuck here. Anyone have any ideas on how I solve this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload