0

How to pass container variable to callback function geomapLoaded?

MyMap = Class.create({
    initialize: function(container) {
            this.container = container;
        google.load('visualization', '1', {'packages': ['geomap'], 'callback' : this.geomapLoaded});
    },

    geomapLoaded: function () {
        this.map      = new google.visualization.GeoMap(this.container);
    }
 }

I am getting this.container undefined in geomapLoaded method (I am using prototype framework).

Rob W
  • 341,306
  • 83
  • 791
  • 678
Jonas
  • 4,683
  • 4
  • 45
  • 81
  • Maybe [closures](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) can help you. – David Mar 18 '11 at 06:36

1 Answers1

1

Like @David said in the comments, you should be able to use a closure to get round this. If you use an anonymous function instead of geomapLoaded then hopefully that will work:

MyMap = Class.create({
    initialize: function(container) {
        this.container = container;

        // Create a reference to this so we can use
        // it in our callback function
        var that = this;

        google.load('visualization', 
            '1', 
            {
                'packages': ['geomap'], 
                'callback' : function() {
                    that.map = new google.visualization.GeoMap(that.container);
                }
            }
        );
    }
 }
Community
  • 1
  • 1
Ian Oxley
  • 10,916
  • 6
  • 42
  • 49