扩展jquery ui小部件 - 如何访问父级事件 [英] Extending jquery ui widget - how to access parent event

查看:81
本文介绍了扩展jquery ui小部件 - 如何访问父级事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个jQuery小部件,该小部件从 ui.slider 扩展而来。我想要一个在'slide'事件上执行的自定义方法。我尝试覆盖父选项,就像正常使用滑块小部件一样,但我遇到了变量范围的问题:

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?

编辑:
链接到ui.slider来源: https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.slider.js

编辑解决方案

$.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,您可以使用 _super 方法:

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

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

superApply

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

这篇关于扩展jquery ui小部件 - 如何访问父级事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆