Extending jquery ui widget - how to access parent event

我是研究僧i 提交于 2020-01-01 05:26:05

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/6992296/extending-jquery-ui-widget-how-to-access-parent-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!