开发者

Instance variables in callback function

开发者 https://www.devze.com 2023-02-18 02:24 出处:网络
How to pass container variable to callback function geomapLoaded? MyMap = Class.create({ initialize: function(container) {

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


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

精彩评论

暂无评论...
验证码 换一张
取 消