Jonas
Jonas

Reputation: 5149

Instance variables in callback function

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).

Upvotes: 0

Views: 1457

Answers (1)

Ian Oxley
Ian Oxley

Reputation: 11056

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);
                }
            }
        );
    }
 }

Upvotes: 1

Related Questions