【问题标题】:Inherit from jQuery UI dialog and call overridden method从 jQuery UI 对话框继承并调用被覆盖的方法
【发布时间】:2011-06-14 22:33:42
【问题描述】:

下面的简单代码描述了我的问题(至少我希望如此):

$.widget("ui.mydialog", $.ui.dialog, {
  _create: function() {
    // How to call _create method of dialog?
  }
});

我尝试在上述创建方法中调用$.ui.dialog.prototype._create(),但在 Firebug 中出现以下错误:

this.element is undefined
this.originalTitle = this.element.attr('title');
jquery...5667348 (line 5864)

我还能怎么称呼那个“超级”方法?

jQuery UI 版本 1.8.8

【问题讨论】:

    标签: javascript jquery jquery-ui inheritance overriding


    【解决方案1】:

    我想我刚刚找到了解决方案...$.ui.dialog.prototype._create.call(this);

    完整代码:

    $.widget("ui.ajaxdialog", $.ui.dialog, {
      _create: function() {
        // Your code before calling the overridden method.
        $.ui.dialog.prototype._create.call(this);
        // Your code after calling the overridden method.
      }
    });
    

    【讨论】:

    • 对于 jqueryui 1.9x 你可以简单的做:this._super('_create'); 上面的解决方案也是有效的。
    • 有趣。 _create 函数中的简单 this._super() 也可以。那么大概就是口味问题了。
    • 实际上,根据文档 (api.jqueryui.com/jQuery.widget/#method-_super),_super 没有任何参数。它只是神奇地知道你在哪里。
    • 每次调用任何“可覆盖”函数时,“this._super”都会更改为函数,它知道它实际指的是哪个“超级”。
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2012-07-28
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多