开发者

Extending jquery ui widget - how to access parent event

开发者 https://www.devze.com 2023-03-27 04:41 出处:网络
I\'m trying to create a jQuery widget which extends from ui.slider. I\'d like to have a custom method which executes on the \'slide\' event. I tried overriding the parent\'s option just like when usin

I'm trying to create a jQuery widget which extends from ui.slider. I'd like to have a custom method which executes on the 'slide' event. I tried overriding the parent's option just like when using the slider widget normally, but I ran into this problem with variable scopes:

$.widget( "ui.myslider", $.ui.slider, {
    _create: function() {
        this.foo = "bar";

        // Outputs "bar"
        this._mySlide();

        // Outputs "undefined" when triggered
        this.options.slide = this._mySlide;

        $.ui.slider.prototype._create.apply(this, arguments);
    },
    _mySlide: function() {
        alert(this.foo);
    }
}

How can I trigger my function on the slide event AND have access to my variable?

Edit: Link to ui.slider source: https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.slider.js

Edit: Solution

$.widget( "ui.myslider", $.ui.slider, {
    _create: function() {
        $.ui.slider.prototype._create.apply(this, arguments);
        this.foo = "bar";
    },
    _slide: function() {
        $.ui.slider.prototype._slide.apply(this, arguments);
     开发者_运维知识库   alert(this.foo);
    }
}


In jQuery UI >= 1.9, you can use the _super method instead:

_slide: function() {
  this._super();
  // equivalent to $.Widget.prototype._slide.call( this );
}

or superApply:

_slide: function() {
  this._superApply(arguments);
  // equivalent to $.Widget.prototype._slide.apply( this, arguments );
}
0

精彩评论

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