开发者

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


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜