jquery widget全局但不是超全局变量

时间:2012-12-02 03:13:42

标签: javascript jquery widget

我希望在拖动后访问变量,但是当我初始化窗口小部件时,$ this和$ opts将被覆盖并始终为'y'。我如何存储和访问正确的变量?

$.widget("custom.test", {

    options: {
        map: ''
    },

    _create: function(){
        $this = this;
        $opts = $this.options;

        if($opts.map){
            $this._createmap();
            $this._loadmap();
        }

        $this._test();
     },

    _test: function(){  
        $map = $this.map;

        $map.drag(function(e, dd) {
            // how to get the correct option here
            // $opts.map is to global?  
        });
    },

    _createmap: function(){
        $this.map = $('<div></div>').addClass('map');
    },
});

$(x).test({map: 'x'});
$(y).test({map: 'y'});

谢谢!

1 个答案:

答案 0 :(得分:0)

有时它很容易

_test: function(){  
    $map = $this.map;

    $map.on('drag',{map: $opts.map}, function(e, dd) {
        // access with event.data
        alert(e.data.map);

    });
},