How to Implement inheritance in Jquery UI widget

早过忘川 提交于 2019-12-24 08:48:10

问题


I am trying to inherit a method from base widget to another widget. here is my sample code

My base widget is

$(function () {
    $.widget("UI.baseWidget", {
        options: {
            testVal:''
        },
        _create: function () {
            alert(this.option.testVal);
        },

    });
});

and other widget calling this base widget is

$(function () {
    $.widget("UI.MyWidget", $.UI.baseWidget, {
        options: {
          testVal:''
        },
        _create: function () {
            $.UI.baseWidget.prototype._create.call(this);
        }

    });
});

and initilise the MyWidgetcode is'

$('#mydiv').MyWidget({testVal:'test widget'})

How can we pass testVal option from MyWidget to baseWidget call? and I getting error some thing like

Uncaught TypeError: i is not a constructor

Uncaught TypeError: $(...).MyWidget is not a function

can please help me to fix this issue. thanks in advance


回答1:


Seems to work OK: I only made one correction : changed option to options in alert(this.option.testVal); and the alert with 'test widget' popped up OK. You can also try to create the widget jquery onReady and see if that fixes the problem i.e. :

$(function () {
    $('#myDiv').MyWidget({testVal:'test widget'});
});

See my code at https://jsfiddle.net/5gmn6x7k/4/



来源:https://stackoverflow.com/questions/39009882/how-to-implement-inheritance-in-jquery-ui-widget

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