https://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/

实例

https://github.com/tkvw/jQuery-File-Upload/blob/master/js/jquery.fileupload-image-editor.js

扩展了

https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload.js

jQuery UI's widget factory makes it easy to build widgets that extend the functionality of existing widgets.

Doing so allows you to build powerful widgets on top of an existing base, as well as make small tweaks to an existing widget's functionality.

Note: This article assumes some basic knowledge of what the widget factory is and how it works. If you're unfamiliar with this, read up on how to use the widget factory first.

link Creating Widget Extensions

如何创建widget

Creating widgets with the widget factory is done by passing the name of the widget and a prototype object to $.widget().

The following creates a "superDialog" widget in the "custom" namespace.

$.widget( "custom.superDialog", {} );

To allow for extension, $.widget() optionally accepts the constructor of a widget to use as a parent.

When specifying a parent widget, pass it as the second argument - after the widget's name, and before the widget's prototype object.

Like the previous example, the following also creates a "superDialog" widget in the "custom" namespace.

However, this time the constructor of jQuery UI's dialog widget ($.ui.dialog) is passed, indicating that the superDialog widget should use jQuery UI's dialog widget as a parent.

$.widget( "custom.superDialog", $.ui.dialog, {} );

Here superDialog and dialog are essentially equivalent widgets with different names and namespaces. To make our new widget more interesting we can add methods to its prototype object.

以jQuery file upload举例,

$.widget('blueimp.fileupload', {

扩展【使用的是下面的Redefining Widgets,不需要创建新的widget】

$.widget('blueimp.fileupload', $.blueimp.fileupload, {

A widget's prototype object is the final argument passed to $.widget().

So far, our examples have been using an empty object. Let's add a method to this object:

$.widget( "custom.superDialog", $.ui.dialog, {
red: function() {
this.element.css( "color", "red" );
}
}); // Create a new <div>, convert it into a superDialog, and call the red() method.
$( "<div>I am red</div>" )
.superDialog()
.superDialog( "red" );

Now the superDialog has a red() method that will change the color of its text to red.

Note how the widget factory automatically sets this to the widget's instance object.

For a full list of the methods and properties available on the instance, see the widget factory's API documentation.

Extending Existing Methods

Sometimes you need to tweak or add to the behavior of existing widget methods.

The do this, specify a method with the same name as the method you want to override on the prototype object.

The following example overrides dialog's open() method. Since dialogs automatically open by default, "open" will be logged when this code runs.

$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" );
}
}); // Create a new <div>, and convert it into a superDialog.
$( "<div>" ).superDialog();

直接添加重名方法,会导致之前父widget的方法被屏蔽,通过_super和_superApply来使得父widget的方法可用

While this runs, there's a problem. Since we overrode the default behavior of open(), the dialog no longer displays on the screen.

When we place methods on the prototype object, we are not actually overriding the original method - rather, we are placing a new method at a higher level in the prototype chain.

To make the parent's methods available, the widget factory provides two methods - _super() and _superApply().

举例https://github.com/ChuckForkJS/jQuery-File-Upload/blob/master/js/jquery.fileupload-image-editor.js#L144  这个是子widget

https://github.com/ChuckForkJS/jQuery-File-Upload/blob/master/js/jquery.fileupload.js#L1288 这个是父widget

Using _super() and _superApply() to Access Parents

_super() and _superApply() invoke methods of the same name in the parent widget.

Refer to the following example. Like the previous one, this example also overrides the open() method to log "open".

However, this time _super() is run to invoke dialog's open() and open the dialog.

$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" ); // Invoke the parent widget's open().
return this._super();
}
}); $( "<div>" ).superDialog();

_super() and _superApply() were designed to behave like the native Function.prototype.call() and Function.prototype.apply() methods.

Therefore, _super() accepts an argument list, and _superApply() accepts a single array of arguments.

区别只是参数问题,一个是参数列表,另外一个是参数数组

This difference is shown in the example below.

$.widget( "custom.superDialog", $.ui.dialog, {
_setOption: function( key, value ) { // Both invoke dialog's setOption() method. _super() requires the arguments
// be passed as an argument list, _superApply() as a single array.
this._super( key, value );
this._superApply( arguments );
}
});

Redefining Widgets

jQuery UI 1.9 added the ability for widgets to redefine themselves.

Therefore, instead of creating a new widget, we can pass $.widget() an existing widget's name and constructor.

The following example adds the same logging in open(), but doesn't create a new widget to do so.

$.widget( "ui.dialog", $.ui.dialog, {
open: function() {
console.log( "open" );
return this._super();
}
}); $( "<div>" ).dialog();

With this approach you can extend an existing widget's method and still have access to the original methods using _super() - all without creating a new widget.

Widgets and Polymorphism 多态

One word of warning when interacting with widget extensions and their plugins.

The parent widget's plugin cannot be used to invoke methods on elements that are child widgets.

This is shown in the example below.

$.widget( "custom.superDialog", $.ui.dialog, {} );

var dialog = $( "<div>" ).superDialog();

// This works.
dialog.superDialog( "close" ); // This doesn't.
dialog.dialog( "close" );

Above, the parent widget's plugin, dialog(), cannot invoke the close() method on an element that is a superDialog.

For more on the invoking widget methods see Widget Method Invocation.

Customizing Individual Instances

All the examples we have looked at so far have extended methods on the widget's prototype.

Methods overridden on the prototype affect all instances of the widget.

To show this, refer to the example below; both instances of the dialog use the same open() method.

$.widget( "ui.dialog", $.ui.dialog, {
open: function() {
console.log( "open" );
return this._super();
}
}); // Create two dialogs, both use the same open(), therefore "open" is logged twice.
$( "<div>" ).dialog();
$( "<div>" ).dialog();

While this is powerful, sometimes you only need to change the behavior for a single instance of the widget.

To do this, obtain a reference to the instance and override the method using normal JavaScript property assignment.

The example below shows this.

var dialogInstance = $( "<div>" )
.dialog() // Retrieve the dialog's instance and store it.
.data( "ui-dialog" ); // Override the close() method for this dialog
dialogInstance.close = function() {
console.log( "close" );
}; // Create a second dialog
$( "<div>" ).dialog(); // Select both dialogs and call close() on each of them.
// "close" will only be logged once.
$( ":data(ui-dialog)" ).dialog( "close" );

This technique of overriding methods for individual instances is perfect for one-off customizations.

Extending Widgets with the Widget Factory的更多相关文章

  1. How To Use the Widget Factory 使用widget factory创建插件

    To start, we'll create a progress bar that just lets us set the progress once.  创建一个基于widget factory ...

  2. jQuery UI Widget Factory

    https://learn.jquery.com/jquery-ui/widget-factory/ The jQuery UI Widget Factory is an extensible bas ...

  3. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  4. Widget Factory (高斯消元解线性方程组)

    The widget factory produces several different kinds of widgets. Each widget is carefully built by a ...

  5. 通过扩展jQuery UI Widget Factory实现手动调整Accordion高度

    □ 实现Accordion高度一致 <head> <meta name="viewport" content="width=device-width&q ...

  6. 【POJ】2947 Widget Factory(高斯消元)

    http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...

  7. POJ Widget Factory 【求解模线性方程】

    传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  8. Why Use the Widget Factory?

    https://learn.jquery.com/jquery-ui/widget-factory/why-use-the-widget-factory/ Writing jQuery plugins ...

  9. POJ 2947:Widget Factory 求同余方程

    Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 5173   Accepted: 1790 De ...

随机推荐

  1. Java中HashMap扩容机制思考

    1. HashMap在什么条件下扩容 判断HashMap的数组Size大小如果超过loadFactor*capacity,就要扩容. 相关的类属性: capacity:当前数组容量,始终保持 2^n, ...

  2. JavaScript ES6 class指南

    前言 EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的 ...

  3. Scrapy 教程(九)-日志系统

    最新版本的 scrapy 已经废弃了 scrapy.log 的使用,赞成显示调用python标准日志记录. Python 内建日志系统 import logging ### python 内建 log ...

  4. Python中函数传递参数有四种形式

    Python中函数传递参数有四种形式 fun1(a,b,c) fun2(a=1,b=2,c=3) fun3(*args) fun4(**kargs) 四种中最常见是前两种,基本上一般点的教程都会涉及, ...

  5. 编辑SE16N表的函数

    函数:SE16N_INTERFACE 此外还可以SE16N 输入对应的查询条件后执行debug该变量 GD-SAPEDIT = ‘X’ 和GD-EDIT = ‘X’ 来实现当前SE16N 中该表的编辑

  6. NSString用法,object-C数组以及字符串拼接和分割

    一.介绍使用NSString创建一个字符串的代码如下: #import <Foundation/Foundation.h>int main (int argc, char *argv[]) ...

  7. django基础篇06-ModelForm操作及验证

    本文内容主要来自银角大王的博客 学习大纲: 一.ModelForm 二.Ajax - 原生(jQuery) - 伪Ajax操作 三.文件上传(预览) - Form提交 - Ajax文件上传 四. 图片 ...

  8. string遍历

    #include <iostream>#include <string> using namespace std;int main(int argc, const char * ...

  9. bzoj5020 & loj2289 [THUWC 2017]在美妙的数学王国中畅游 LCT + 泰勒展开

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5020 https://loj.ac/problem/2289 题解 这个 appear 和 d ...

  10. 关于web前端性能优化问题

    好久没有来博客写随笔了,可能是懒了吧,哈哈, 最近很想整理一篇关于前端性能优化的问题,毕竟能提高网站的观赏性对吧.提升网站性能,提升用户体验 那 什么是web性能优化? 可以这么理解:从用户访问资源到 ...