eschwartz / backbone.googlemaps

A Backbone JS extension for interacting with the Google Maps API (v3.10)
MIT License
139 stars 55 forks source link

latlng changed before gOverlay not completely created #23

Closed twhtanghk closed 10 years ago

twhtanghk commented 10 years ago

I try to create MarkerViewCollection with a location added into the collection. It would then trigger addChild to render the MarkerView.gOverlay. Before calling render, another update on the location is triggered and then invoke MarkerView.refreshOverlay before invoking the render. It then throws an exception to reference undefined MarkerView.gOverlay.

It is suggested to add a condition to check gOverlay ready or not before trigger the action model <--eventhandler--> update overlay. Any other suggestion.

    // update overlay position if lat or lng change
    refreshOverlay: function() {
      // Only update overlay if we're not already in sync
      // Otherwise we end up in an endless loop of
      // update model <--eventhandler--> update overlay
      if(this.gOverlay && !this.model.getLatLng().equals(this.gOverlay.getPosition())) {
        this.gOverlay.setOptions({
          position: this.model.getLatLng()
        });
      }
    },
eschwartz commented 10 years ago

The gOverlay property is created in the MarkerView constructor, so it should be available before render is called. The only reason I can see that gOverlay would be falsey is that the view would have been previously closed.

Note that closing a view actually destroys the view, its properties, and it's event bindings. If you're trying to hide a view, you might consider creating a hide method like so:

MyMarkerView = MarkerView.extend({
  hide: function() {
    this.gOverlay.setVisible(false);
  }
});

Are you able to duplicate the issue with a code snippet? It may help clarify the problem.

eschwartz commented 10 years ago

Closing for inactivity. Let me know if you're still having issues.