目录

JqueryUI - Widget Factory( Widget Factory)

早些时候,在jQuery中编写自定义控件的唯一方法是扩展$.fn命名空间。 这适用于简单的小部件。 假设您构建了更多有状态的小部件,它很快变得很麻烦。 为了帮助构建小部件,Widget Factory在jQuery UI中引入,它删除了通常与管理小部件相关的大部分样板。

jQueryUI Widget Factory只是一个函数($ .widget),它将字符串名称和对象作为参数,并创建一个jQuery插件和一个“Class”来封装其功能。

语法 (Syntax)

以下是jQueryUI Widget Factory方法的语法 -

jQuery.widget( name [, base ], prototype )

name - 它是一个字符串,包含要创建的窗口小部件的namespacewidget name (用点分隔)。

base - 要继承的基本窗口小部件。 这必须是可以使用`new`关键字实例化的构造函数。 默认为jQuery.Widget

prototype - 要用作要继承的窗口小部件prototype的对象。 例如,jQuery UI有一个“鼠标”插件,其余的交互插件都基于该插件。 为了实现这一点, draggable, droppable,等都继承自鼠标插件,如:jQuery.widget(“ui.draggable”,$ .ui.mouse,{...}); 如果你不提供这个参数,那么widget将直接从“基本小部件”继承jQuery.Widget(注意小写“w”jQuery.widget和大写“W”jQuery.Widget之间的区别)。

基本小工具

基本小部件是小部件工厂使用的小部件。

选项 (Options)

下表列出了可与基本窗口小部件一起使用的不同options -

Sr.No. 选项和说明
1 disabledhide

如果设置为true则此选项将禁用窗口小部件。 默认情况下,其值为false

Option - disabledhide

如果设置为true则此选项将禁用窗口小部件。 默认情况下,其值为false

Example

$( ".selector" ).widget({ disabled: true });
2 hide

此选项确定如何为元素的隐藏设置动画。 默认情况下,其值为null

Option - hide

此选项确定如何为元素的隐藏设置动画。 默认情况下,其值为null

这可以是 -

  • Boolean - 如果设置为false则不使用动画。 如果设置为true则元素将使用默认持续时间和默认缓动淡出。

  • Number - 元素将以指定的持续时间和默认缓动淡出。

  • String - 将使用指定的效果隐藏元素。

  • Object - 如果值是对象,则可以提供effect, delay, duration, easing属性。

Example

$( ".selector" ).widget({ hide: { effect: "explode", duration: 1000 } });
3 show

此选项确定如何为元素的显示设置动画。 默认情况下,其值为null

Option - show

此选项确定如何为元素的显示设置动画。 默认情况下,其值为null

这可以是 -

  • Boolean - 如果设置为false不会使用动画来显示元素。 如果设置为true则元素将以默认持续时间和默认缓动淡入。

  • Number - 元素将以指定的持续时间和默认缓动淡入。

  • String - 将使用指定的效果显示元素。

  • Object - 如果值是对象,则可以提供effect, delay, duration, easing属性。

Example

$( ".selector" ).widget({ show: { effect: "explode", duration: 1000 } });

方法 (Methods)

下表列出了可与基本窗口小部件一起使用的不同methods -

Sr.No. 行动和描述
1 _create()

这个方法是widget的构造函数。 没有参数,但是已经设置了this.elementthis.options

Action - _create()

此动作完全破坏元素的手风琴功能。 元素返回到pre-init状态。 这个方法是widget的构造函数。 没有参数,但是已经设置了this.elementthis.options

Example

_create: function() {
   this.element.css( "background-color", this.options.color );
}
2 _delay(fn [,延迟])

此方法在指定的延迟后调用提供的函数。 返回与clearTimeout()一起使用的超时ID。

Action - _delay( fn [, delay ] )

此方法在指定的延迟后调用提供的函数。 返回与clearTimeout()一起使用的超时ID。

Example

this._delay( this._foo, 100 );
3 _destroy()

public destroy()方法清除所有公共数据,事件等,然后委托给这个_destroy()方法进行自定义,特定于小部件的清理。

Action - _destroy()

public destroy()方法清除所有公共数据,事件等,然后委托给这个_destroy()方法进行自定义,特定于小部件的清理。

Example

_destroy: function() {
   this.element.removeClass( "my-widget" );
}
4 _focusable( element )

此方法设置元素以将ui-state-focus类应用于焦点。 事件处理程序会在销毁时自动清理。

Action - _focusable( element )

此方法设置元素以将ui-state-focus类应用于焦点。 事件处理程序会在销毁时自动清理。

Example

_create: function() {
   this._focusable( this.element.find( ".my-items" ) );
}
5 _getCreateEventData()

所有小部件都会触发create事件。 默认情况下,事件中不提供任何数据,但此方法可以返回将作为create事件数据传递的对象。

Action - _getCreateEventData()

所有小部件都会触发create事件。 默认情况下,事件中不提供任何数据,但此方法可以返回将作为create事件数据传递的对象。

Example

_getCreateEventData: function() {
   return this.options;
}
6 _getCreateOptions()

此方法允许窗口小部件定义用于在实例化期间定义选项的自定义方法。 用户提供的选项会覆盖此方法返回的选项,这些选项会覆盖默认选项。

Action - _getCreateOptions()

此方法允许窗口小部件定义用于在实例化期间定义选项的自定义方法。 用户提供的选项会覆盖此方法返回的选项,这些选项会覆盖默认选项。

Example

_getCreateOptions: function() {
   return { id: this.element.attr( "id" ) };
}
7 _hide(元素,选项[,回调])

此方法使用内置动画方法或使用自定义效果立即隐藏元素。 有关可能的选项值,请参阅hide选项。

Action - _hide( element, option [, callback ] )

此方法使用内置动画方法或使用自定义效果立即隐藏元素。 有关可能的选项值,请参阅hide选项。

Example

this._hide( this.element, this.options.hide, function() {
   $( this ).remove();
});
8 _hoverable( element )

此方法设置元素以在悬停时应用ui-state-hover类。 事件处理程序会在销毁时自动清理。

Action - _hoverable( element )

此方法设置元素以在悬停时应用ui-state-hover类。 事件处理程序会在销毁时自动清理。

Example

this._hoverable( this.element.find( "div" ) );
9 _init()

每次调用插件时都没有参数或只有一个选项哈希,小部件被初始化; 这包括创建窗口小部件的时间。

Action - _init()

每次调用插件时都没有参数或只有一个选项哈希,小部件被初始化; 这包括创建窗口小部件的时间。

Example

_init: function() {
   if ( this.options.autoOpen ) {
      this.open();
   }
}
10 _off(element,eventName)

此方法取消绑定指定元素的事件处理程序。

Action - _off( element, eventName )

此方法取消绑定指定元素的事件处理程序。

Example

this._off( this.element, "click" );
11 _on([suppressDisabledCheck] [,element],处理程序)

将事件处理程序绑定到指定的元素。 通过事件名称内的选择器支持委派,例如“click .foo”。

Action - _on( [suppressDisabledCheck ] [, element ], handlers )

将事件处理程序绑定到指定的元素。 通过事件名称内的选择器支持委派,例如“click .foo”。

Example

this._on( this.element, {
   "click a": function( event ) {
      event.preventDefault();
   }
});
12 _setOption( key, value )

对于每个单独的选项,从_setOptions()方法调用此方法。 应根据更改更新窗口小部件状态。

Action - _setOption( key, value )

对于每个单独的选项,从_setOptions()方法调用此方法。 应根据更改更新窗口小部件状态。

Example

_setOption: function( key, value ) {
   if ( key === "width" ) {
      this.element.width( value );
   }
   if ( key === "height" ) {
      this.element.height( value );
   }
   this._super( key, value );
}
13 _setOptions( options )

无论调用option()方法的形式如何,只要调用了option()方法,就会调用此方法。

Action - _setOptions( options )

无论调用option()方法的形式如何,只要调用了option()方法,就会调用此方法。

Example

_setOptions: function( options ) {
   var that = this,
   resize = false;
   $.each( options, function( key, value ) {
      that._setOption( key, value );
      if ( key === "height" || key === "width" ) {
         resize = true;
      }
   });
   if ( resize ) {
      this.resize();
   }
}
14 _show(element,option [,callback])

使用内置动画方法或使用自定义效果立即显示元素。 有关可能的选项值,请参阅show选项。

Action - _show( element, option [, callback ] )

使用内置动画方法或使用自定义效果立即显示元素。 有关可能的选项值,请参阅show选项。

Example

_this._show( this.element, this.options.show, function() {
   // Focus the element when it's fully visible.
   this.focus();
}
15 _super([arg] [,...])

此方法使用任何指定的参数从父窗口小部件调用相同名称的方法。 基本上.call()。

Action - _super( [arg ] [, ... ] )

此方法使用任何指定的参数从父窗口小部件调用相同名称的方法。 基本上.call()。

Example

_setOption: function( key, value ) {
   if ( key === "title" ) {
      this.element.find( "h3" ).text( value );
   }
   this._super( key, value );
}
16 _superApply( arguments )

使用参数数组从父窗口小部件调用相同名称的方法。

Action - _superApply( arguments )

使用参数数组从父窗口小部件调用相同名称的方法。

Example

_setOption: function( key, value ) {
   if ( key === "title" ) {
      this.element.find( "h3" ).text( value );
   }
   this._superApply( arguments );
}
17 _trigger(type [,event] [,data])

此方法触发事件及其关联的回调。 名称等于type的选项将作为回调调用。

Action - _trigger( type [, event ] [, data ] )

此方法触发事件及其关联的回调。 名称等于type的选项将作为回调调用。

Example

this._on( this.element, {
   keydown: function( event ) {
      // Pass the original event so that the custom search event has
      // useful information, such as keyCode
      this._trigger( "search", event, {
         // Pass additional information unique to this event
         value: this.element.val()
      });
   }
});
18 destroy()

此方法完全删除窗口小部件功能。 这将使元素返回到pre-init状态。

Action - destroy()

此方法完全删除窗口小部件功能。 这将使元素返回到pre-init状态。

Example

this._on( this.element, {
   "click a": function( event ) {
      event.preventDefault();
      this.destroy();
   }
});
19 disable()

此方法禁用窗口小部件。

Action - disable()

此方法禁用窗口小部件。

Example

this._on( this.element, {
   "click a": function( event ) {
      event.preventDefault();
      this.disable();
   }
});
20 enable()

此方法启用窗口小部件。

Action - enable()

此方法启用窗口小部件。

Example

this._on( this.element, {
   "click a": function( event ) {
      event.preventDefault();
      this.enable();
   }
});
21 option( optionName )

此方法获取当前与指定的optionName关联的值。

Action - option( optionName )

此方法获取当前与指定的optionName关联的值。

Example

this.option( "width" );
22 option()

此方法获取一个对象,该对象包含表示当前窗口小部件选项哈希的键/值对。

Action - option()

此方法获取一个对象,该对象包含表示当前窗口小部件选项哈希的键/值对。

Example

var options = this.option();
for ( var key in options ) {
   console.log( key, options[ key ] );
}
23 option( optionName, value )

此方法设置与指定的optionName关联的窗口小部件选项的值。

Action - option( optionName, value )

此方法设置与指定的optionName关联的窗口小部件选项的值。

Example

this.option( "width", 500 );
24 option( options )

此方法为窗口小部件设置一个或多个选项。

Action - option( options )

此方法为窗口小部件设置一个或多个选项。

Example

this.option({
   width: 500,
   height: 500
});
25 widget()

此方法返回包含原始元素或其他相关生成元素的jQuery对象。

Action - widget()

此方法返回包含原始元素或其他相关生成元素的jQuery对象。

Example

_create: function() {
   this.widget().css( "border", "2px solid red" );
}

事件 (Events)

Sr.No. 事件方法和描述
1 create( event, ui )

创建窗口小部件时会触发此事件。

Event - create( event, ui )

创建窗口小部件时会触发此事件。 其中eventEvent类型, uiObject类型。

Syntax

$( ".selector" ).widget({
   create: function( event, ui ) {}
});

jQueryUI小部件工厂生命周期

jQueryUI小部件工厂提供了一种面向对象的方式来管理小部件的生命周期。 这些生命周期活动包括 -

创建和销毁小部件:例如,

$( "#elem" ).progressbar();

更改窗口小部件选项:例如

$( "#elem" ).progressbar({ value: 20 });

在子类窗口小部件中进行“超级”调用:例如

$( "#elem" ).progressbar( "value" );
or 
$( "#elem" ).progressbar( "value", 40 );

事件通知:例如

$( "#elem" ).bind( "progressbarchange", function() {
   alert( "The value has changed!" );
});

例子 (Example)

现在让我们在以下示例中创建自定义小部件。 我们将创建一个按钮小部件。 我们将在以下示例中看到如何在窗口小部件中创建选项,方法和事件 -

创建自定义小组件

我们先创建一个简单的自定义小部件。

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Widget - Default functionality</title>
      <link rel = "stylesheet" href = "//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <script>
         $(function() {
            $.widget("iP.myButton", {
               _create: function() { 
                  this._button = $("<button>"); 
                  this._button.text("My first Widget Button");
                  this._button.width(this.options.width) 
                  this._button.css("background-color", this.options.color);    
                  this._button.css("position", "absolute");   
                  this._button.css("left", "100px");            
                  $(this.element).append(this._button);
               },
            });
            $("#button1").myButton();
         });
      </script>
   </head>
   <body>
      <div id = "button1"></div>
   </body>
</html>

让我们将上面的代码保存在一个HTML文件widgetfactoryexample.htm ,并在支持javascript的标准浏览器中打开它,你还必须看到以下输出 -

新页面打开

将选项添加到自定义小组件

在前面的示例中,我们使用_create函数来创建自定义控件。 但是用户通常希望通过设置和修改选项来自定义控件。 我们可以定义一个选项对象,它存储您定义的所有选项的默认值。 _setOption函数用于此目的。 它针对用户设置的每个单独选项进行调用。 这里我们设置按钮的widthbackground-color

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Widget - Default functionality</title>
      <link rel = "stylesheet" href = "//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <script>
         $(function() {
            $.widget("iP.myButton", {
               _create: function() { 
                  this._button = $("<button>"); 
                  this._button.text("My first Widget Button");
                  this._button.width(this.options.width) 
                  this._button.css("background-color", this.options.color);    
                  this._button.css("position", "absolute");   
                  this._button.css("left", "100px");            
                  $(this.element).append(this._button);
               },
               _setOption: function(key, value) { 
                  switch (key) { 
                     case "width": 
                     this._button.width(value); 
                     break; 
                     case "color":
                     this._button.css("background-color",value);
                     break; 
                  } 
               },
            });
            $("#button2").myButton();
            $("#button2").myButton("option", {width:100,color:"#cedc98"});
         });
      </script>
   </head>
   <body>
      <div id = "button2"></div>
   </body>
</html>

让我们将上面的代码保存在一个HTML文件widgetfactoryexample.htm ,并在支持javascript的标准浏览器中打开它,你还必须看到以下输出 -

新页面打开

将方法添加到自定义小组件

在下面的示例中,我们将添加用户可以使用的方法,这些方法很容易构建到框架中。 我们将编写一个Move方法,将按钮移动指定的水平距离。 为了使这个工作,我们还需要在_create函数中设置position和left属性 -

this._button.css("position", "absolute");   
this._button.css("left", "100px");  

在此之后,用户现在可以使用通常的jQuery UI方式调用您的方法 -

this._button.css("position", "absolute");   
this._button.css("left", "100px");  
$("button3").myButton("move", 200);
<!DOCTYPE html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Widget - Default functionality</title>
      <link rel = "stylesheet" href = "//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <script>
         $(function() {
            $.widget("iP.myButton", {
               _create: function() { 
                  this._button = $("<button>"); 
                  this._button.text("My first Widget Button");
                  this._button.width(this.options.width) 
                  this._button.css("background-color", this.options.color);    
                  this._button.css("position", "absolute");   
                  this._button.css("left", "100px");            
                  $(this.element).append(this._button);
               },
               move: function(dx) { 
                  var x = dx + parseInt(this._button.css("left")); 
                  this._button.css("left", x); 
                  if(x>400) { this._trigger("outbounds",{},  {position:x}); }
               }
            });
            $("#button3").myButton();
            $("#button3").myButton("move", 200);
         });
      </script>
   </head>
   <body>
      <div id = "button3"></div>
   </body>
</html>

让我们将上面的代码保存在一个HTML文件widgetfactoryexample.htm ,并在支持javascript的标准浏览器中打开它,你还必须看到以下输出 -

新页面打开

将事件添加到自定义小组件

在此示例中,我们将演示如何创建事件。 要创建事件,您只需使用_trigger方法即可。 第一个参数是事件的名称,第二个是要传递的标准事件对象,第三个是要传递的自定义事件对象。

如果按钮移动到x = 400以上,我们将触发一个事件。 你所要做的就是添加移动功能 -

if(x<400) { this._trigger("outbounds",{}, {position:x}); }

在这种情况下,事件被称为outbounds,并且一个空事件对象与一个自定义事件对象一起传递,该事件对象仅将该位置作为其唯一属性提供。

整个移动功能是 -

move: function(dx) {
   var x = dx + parseInt(this._button.css("left")); 
   this._button.css("left", x); 
   if(x<400) { this._trigger("outbounds",{}, {position:x}); }
}

用户可以通过简单地定义相同名称的选项来设置事件处理功能。

$("button4").myButton("option", {
   width: 100, 
   color: "red",
   outbounds:function(e,ui) {
      alert(ui.position);}
});
<!DOCTYPE html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Widget - Default functionality</title>
      <link rel = "stylesheet" href = "//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <script>
         $(function() {
            $.widget("iP.myButton", {
               _create: function() { 
                  this._button = $("<button>"); 
                  this._button.text("My first Widget Button");
                  this._button.width(this.options.width) 
                  this._button.css("background-color", this.options.color);    
                  this._button.css("position", "absolute");   
                  this._button.css("left", "100px");            
                  $(this.element).append(this._button);
               },
               move: function(dx) { 
                  var x = dx + parseInt(this._button.css("left")); 
                  this._button.css("left", x); 
                  if(x>400) { this._trigger("outbounds",{},  {position:x}); }
               }
            });
            $("#button4").myButton();
            $("#button4").on("mybuttonoutbounds", function(e, ui) {
               alert("out");
            });
            $("#button4").myButton("move", 500);
         });
      </script>
   </head>
   <body>
      <div id = "button4"></div>
   </body>
</html>

让我们将上述代码保存在HTML文件widgetfactoryexample.htm ,并在支持javascript的标准浏览器中打开它,打开一个警告框。

新页面打开
↑回到顶部↑
WIKI教程 @2018