6

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 Answer 1

10

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 );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.