eschwartz / backbone.googlemaps

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

Event for when the pins have been added to the dom #28

Closed halcyonandon closed 10 years ago

halcyonandon commented 10 years ago

Hi, I'm using richmarker, but instead of setting the visibility of the pin, I'm adding classes. The purpose of this is to use css transitions for the drop in effect.

Where is the appropriate place to do this? is this onRender?

halcyonandon commented 10 years ago

Nevermind... ready did the trick... since this isn't in the example code, for anyone that may end up looking for this, my solution was as follows:

  var MarkerView = Backbone.GoogleMaps.MarkerView.extend({
    infoWindow: InfoWindow,
    initialize: function() {
      //_.bindAll(this, 'handleDragEnd');
    },
    mapEvents: {
      ready: 'markerReady',
      click: 'pinClick'
    },
    markerReady: function() {
      // This is the equivalent of gmaps.event.addListener( spot.marker, 'ready', function(event) { ... })
      var index = this.model.attributes.index;
      if (!$.browser.mobile) {
        // Hover function
        $("#label" + index).mouseenter(function() {
          $('.result').removeClass('hovered');
          $(".result" + index).addClass('hovered');
        });
        $("#label" + index).mouseleave(function() {
          $(".result" + index).removeClass('hovered');
        });
      }
      $.wait(index*30).then(function() {
        $('#label'+ index).addClass('display-pin animated bounceInDown');
      });
    },

but unfortunately that click event isn't triggering for some reason

eschwartz commented 10 years ago

One thing you may be running into is that the library is setting the mapEvents object as a property of the MarkerView prototype rather than of each MarkerView instance. This can cause unwanted side effects, if any one instance changes their mapEvents object.

I just fixed this in e8a52a5a57b6d5afc4837e270466cde7e73ddd93. You'll want to follow suit. So instead of:

 var MarkerView = Backbone.GoogleMaps.MarkerView.extend({
  // A single mapEvents object is assigned to MarkerView.prototype by Backbone.extend
  // If this object changes, every instance of MarkerView will be effected
  mapEvents: {...}
});

you'll want do this:

var MarkerView = Backbone.GoogleMaps.MarkerView.extend({
  initialize: function() {
    // Every instance gets its own version of the mapEvents object
    this.mapEvents = { ... }
  }
});

I hope that helps.