jQuery UI API - Parte Gallery (Widget Factory)
categoria
Utility (Utilità) | widgets (Widget)
jQuery.widget (nome [, base], prototipo) Utilizzo
Descrizione: Utilizzare tutti i widget jQuery UI con la stessa astrazione per creare stateful plugin per jQuery.
参数 | 类型 | 类型 |
---|---|---|
name | String | 要创建的小部件名称,包括命名空间。 |
base | Function() | 要继承的基础小部件。必须是一个可以使用 `new` 关键词实例化的构造函数。默认为 jQuery.Widget。 |
prototype | PlainObject | 要作为小部件原型使用的对象。 |
È possibile utilizzare il $.Widget
oggetto come alla base di eredità, o può essere esplicitamente o jQuery UI da un controllo di terze parti esistente, creato da zero un nuovo widget. La definizione di un widget con lo stesso nome di ereditare l'elemento di base, e permette anche di espandere correttamente widget.
jQuery UI include molti sono in possesso di piccole parti, in modo che il tipico plugin jQuery leggermente diversi modelli di utilizzo. Tutti i widget jQuery UI utilizzando lo stesso modello, che si compone di librerie membri (Widget Factory) definizione. Quindi, fino a quando si impara a utilizzare uno, si sa come utilizzare altri widget (Widget).
Nota: questa sezione utilizzando il membro barra di avanzamento (Progressbar Widget) La manifestazione, ma la sintassi è applicata a ciascun widget.
inizializzazione
Al fine di monitorare lo stato del widget, dobbiamo introdurre l'intero ciclo di vita del widget. Ciclo di vita inizia quando il widget di inizializzazione. Per inizializzare un widget, abbiamo semplicemente bisogno di collegare una chiamata o una pluralità di elementi.
$ ( "#elem") .progressbar ();
Ciò inizializzare l'oggetto jQuery per ogni elemento. Esempi di elementi di cui sopra con id "elem"
.
Opzioni
Dal momento che progressbar()
senza parametri quando si chiama, il widget viene inizializzato utilizzando le opzioni di default. Siamo in grado di superare una serie di opzioni per ignorare l'impostazione predefinita nelle opzioni di inizializzazione:
$ ( "#elem") .progressbar ({Valore: 20});
Potremmo aver bisogno di passare in base al numero di opzioni, non abbiamo passato una delle opzioni sono di utilizzare i loro valori di default.
È possibile passare opzioni multiple di parametri che verranno uniti in un unico oggetto (come $.extend( true, target, object1, objectN )
). Questo numero è impostato per coprire tutti i casi, la condivisione tra le istanze opzione è utile:
var options = {modal: true, spettacolo: "lento"}; $ ( "# Dialog1") .dialog (opzioni); $ ( "# Dialog2") .dialog (opzioni, {AutoOpen: false});
Tutte le opzioni sono consegnati durante la copia profonda inizializzazione, assicurando il follow-up non pregiudica la piccola parte del caso per modificare l'oggetto. Array è l'unica eccezione, come sono per riferimento. L'eccezione è per supportare correttamente l'associazione dati, in cui la sorgente di dati deve essere utilizzato come riferimento.
Il valore predefinito è memorizzato nelle proprietà del widget, in modo da poter coprire il valore delle impostazioni jQuery UI. Ad esempio, dopo le seguenti impostazioni, tutte le future istanze di barra di avanzamento per default il valore di 80:
$ .ui.progressbar.prototype.options.value = 80;
Le opzioni sono parte di un piccolo Stato membro, in modo che possiamo anche impostare le opzioni dopo l'inizializzazione. Vedremo l'opzione nei metodi successive.
modo
Ora il widget è stato inizializzato, siamo in grado di interrogare il suo status, o eseguire azioni su widget. Tutte le azioni sono basate sul metodo di inizializzazione dopo è chiamato a svolgere. Per richiamare un metodo sul widget, passiamo il nome del metodo al plugin jQuery. Ad esempio, chiedendo agli Stati barra di avanzamento (Progressbar il Widget) value()
metodo, possiamo utilizzare:
$ ( "#elem") .progressbar ( "Valore");
Se il metodo accetta parametri, siamo in grado di passare i parametri dopo il nome del metodo. Ad esempio, per passare un parametro 40
al value()
metodo, possiamo utilizzare:
$ ( "#elem") .progressbar ( "Valore", 40);
Come jQuery in altri modi, metodo più widget restituisce un oggetto jQuery:
$ ( "#elem") .progressbar ( "valore", 90) .addClass ( "quasi-fatto");
Ogni widget ha le proprie impostazioni del metodo, che si basano sulla funzionalità fornita dal widget. Tuttavia, ci sono alcuni modi è presente su tutte le piccole parti, che saranno spiegate in dettaglio qui di seguito.
evento
Tutti i widget e hanno una varietà di eventi comportamentali legate al fine di notificare quando un cambiamento di stato. Per la maggior parte piccole parti, quando l'evento viene attivato, il nome in lettere minuscole, sotto forma di un nome di widget come prefisso. Ad esempio, siamo in grado di legare la barra di avanzamento change
evento, che viene attivato quando il valore cambia.
$ ( "#elem") .bind ( "Progressbarchange", function () { alert ( "Il valore è cambiato!"); });
Ogni evento ha un corrispondente ritiro, che come opzione. Se necessario, si può afferrare il progresso del change
callback, piuttosto che vincolanti progressbarchange
evento.
$ ( "#elem") .progressbar ({ cambiare: function () { alert ( "Il valore è cambiato!"); } });
Tutti i widget hanno un change
evento, che viene attivato quando un'istanza.
instantiation
Esempi di widget è quello di utilizzare il nome completo con una piccola parte come chiave jQuery.data()
negozio. Pertanto, è possibile utilizzare il seguente codice dall'elemento per recuperare l'oggetto istanza membro barra di avanzamento (Progressbar Widget).
$ ( "#elem") .data ( "Ui-progressbar");
Se l'elemento è legato per un dato widget, è possibile utilizzare :data
di selezione per individuare.
$ ( "#elem") .IS ( ": Dati ( 'ui-progressbar')"); // true $ ( "#elem") .IS ( ": Dati ( 'ui-trascinabile')"); // false
È inoltre possibile utilizzare :data
per ottenere un elenco di un determinato oggetto quando una lista di tutti gli elementi.
$ ( ": Dati ( 'ui-progressbar')");
proprietà
Tutti i widget hanno i seguenti attributi:
- defaultElement: elementi durante la costruzione della non disponibili elementi widget di istanza da utilizzare. Ad esempio, poiché la barra di avanzamento
defaultElement
è"<div>
",$.ui.progressbar({ value: 50 })
in un nuovo<div>
nell'istanza del widget barra di avanzamento esempio. - documento: contiene piccole parti all'interno dell'elemento
document
. Utile se avete bisogno di interagire con, nel quadro del widget. - elemento: un oggetto jQuery che contiene gli elementi utilizzati per creare un'istanza di widget. Se si selezionano più elementi e chiamare
.myWidget()
, si creerà una singola istanza Widget di ogni elemento. Pertanto, la proprietà contiene sempre un elemento. - namespace: Posizione widget di prototipo negozio oggetto globale jQuery. Ad esempio,
"ui"
unnamespace
rappresenta una piccola prototipi vengono memorizzati in$.ui
. - opzioni: la possibilità di utilizzare un oggetto contiene attualmente i widget. Quando un'istanza, una delle opzioni fornite dall'utente saranno automaticamente e
$.myNamespace.myWidget.prototype.options
unire default definiti. opzioni specificate dall'utente sovrascrivono i valori di default. - uuid: un identificatore unico intero indica il controllo.
- Versione: la versione stringa del widget. Per i widget jQuery UI, la proprietà verrà impostata alla versione di widget jQuery UI utilizzata. Gli sviluppatori plug-in deve impostare in modo esplicito la proprietà del loro prototipo.
- widgetEventPrefix: Aggiungere i widget per il prefisso del nome della manifestazione. Ad esempio, possibile trascinare e rilasciare i widget (Draggable Widget) di
widgetEventPrefix
è"drag"
, in modo che quando si crea un trascinabili, il nome della manifestazione è"dragcreate"
. Per impostazione predefinita, il widgetwidgetEventPrefix
è il suo nome. Nota: Questa proprietà è stata abbandonata, per lo futura release. Il nome dell'evento è cambiato in widgetName: eventName (come ad esempio"draggable:create"
). - widgetFullName: widget di contiene il nome completo dello spazio dei nomi. Per
$.widget( "myNamespace.myWidget", {} )
,widgetFullName
sarà"myNamespace-myWidget"
. - widgetName: il nome del widget. Per
$.widget( "myNamespace.myWidget", {} )
,widgetName
sarà"myWidget"
. - Finestra: contiene piccole parti all'interno dell'elemento
window
. Utile se avete bisogno di interagire con, nel quadro del widget.
widget di base jQuery.Widget utilizzo
Descrizione: La libreria di base widget di Widget (Widget Factory) uso.
navigazione rapida
选项 | 方法 | 事件 |
---|---|---|
选项 | 类型 | 描述 | 默认值 |
---|---|---|---|
disabled | Boolean | 如果设置为 true ,则禁用该小部件。代码实例: 初始化带有指定 $( ".selector" ).widget({ disabled: true }); 在初始化后,获取或设置 // getter var disabled = $( ".selector" ).widget( "option", "disabled" ); // setter $( ".selector" ).widget( "option", "disabled", true ); |
false |
hide | Boolean 或 Number 或 String 或 Object | 是否使用动画隐藏元素,以及如何动画隐藏元素。 支持多个类型:
代码实例: 初始化带有指定 $( ".selector" ).widget({ hide: { effect: "explode", duration: 1000 } }); 在初始化后,获取或设置 // getter var hide = $( ".selector" ).widget( "option", "hide" ); // setter $( ".selector" ).widget( "option", "hide", { effect: "explode", duration: 1000 } ); |
null |
show | Boolean 或 Number 或 String 或 Object | 是否使用动画显示元素,以及如何动画显示元素。 支持多个类型:
代码实例: 初始化带有指定 $( ".selector" ).widget({ show: { effect: "blind", duration: 800 } }); 在初始化后,获取或设置 // getter var show = $( ".selector" ).widget( "option", "show" ); // setter $( ".selector" ).widget( "option", "show", { effect: "blind", duration: 800 } ); |
null |
方法 | 返回 | 描述 |
---|---|---|
_create() | jQuery (plugin only) | _create() 方法是小部件的构造函数。没有参数,但是 this.element 和 this.options 已经设置。
代码实例: 基于一个选项设置小部件元素的背景颜色。 _create: function() { this.element.css( "background-color", this.options.color ); } |
_delay( fn [, delay ] ) | Number | 在指定延迟后调用提供的函数。保持 this 上下文正确。本质上是 setTimeout() 。使用 clearTimeout() 返回超时 ID。
代码实例: 100 毫秒后在小部件上调用 this._delay( this._foo, 100 ); |
_destroy() | jQuery (plugin only) | 公共的 destroy() 方法清除所有的公共数据、事件等等。代表了定制、指定小部件、清理的 _destroy() 。
代码实例: 当小部件被销毁时,从小部件的元素移除一个 class。 _destroy: function() { this.element.removeClass( "my-widget" ); } |
_focusable( element ) | jQuery (plugin only) | 建立聚焦在元素上时要应用 ui-state-focus class 的 element 。
代码实例: 向小部件内的一组元素应用 focusable 样式: this._focusable( this.element.find( ".my-items" ) ); |
_getCreateEventData() | Object | 所有的小部件触发 create 事件。默认情况下,事件中不提供任何的数据,但是该方法会返回一个对象,作为 create 事件的数据被传递。
代码实例: 向 _getCreateEventData: function() { return this.options; } |
_getCreateOptions() | Object | 该方法允许小部件在初始化期间为定义选项定义一个自定义的方法。用户提供的选项会覆盖该方法返回的选项,即会覆盖默认的选项。
代码实例: 让小部件元素的 id 属性作为选项可用。 _getCreateOptions: function() { return { id: this.element.attr( "id" ) }; } |
_hide( element, option [, callback ] ) | jQuery (plugin only) | 使用内置的动画方法或使用自定义的特效隐藏一个元素。如需了解可能的 option 值,请查看 hide 。
代码实例: 为自定义动画传递 this._hide( this.element, this.options.hide, function() { // Remove the element from the DOM when it's fully hidden. $( this ).remove(); }); |
_hoverable( element ) | jQuery (plugin only) | 建立悬浮在元素上时要应用 ui-state-hover class 的 element 。事件处理程序在销毁时自动清理。
代码实例: 当悬浮在元素上时,向元素内所有的 this._hoverable( this.element.find( "div" ) ); |
_init() | jQuery (plugin only) | 小部件初始化的理念与创建不同。任何时候不带参数的调用插件或者只带一个选项哈希的调用插件,初始化小部件。当小部件被创建时会包含这个方法。
注释:如果存在不带参数成功调用小部件时要执行的逻辑动作,初始化只能在这时处理。
代码实例: 如果设置了 _init: function() { if ( this.options.autoOpen ) { this.open(); } } |
_off( element, eventName ) | jQuery (plugin only) | 从指定的元素取消绑定事件处理程序。
代码实例: 从小部件的元素上取消绑定所有 click 事件。 this._off( this.element, "click" ); |
_on( [suppressDisabledCheck ] [, element ], handlers ) | jQuery (plugin only) | 授权通过事件名称内的选择器被支持,例如 "click .foo" 。 _on() 方法提供了一些直接事件绑定的好处:
代码实例: 放置小部件元素内所有被点击的链接的默认行为。 this._on( this.element, { "click a": function( event ) { event.preventDefault(); } }); |
_setOption( key, value ) | jQuery (plugin only) | 为每个独立的选项调用 _setOptions() 方法。小部件状态随着改变而更新。
代码实例: 当小部件的 _setOption: function( key, value ) { if ( key === "width" ) { this.element.width( value ); } if ( key === "height" ) { this.element.height( value ); } this._super( key, value ); } |
_setOptions( options ) | jQuery (plugin only) | 当调用 option() 方法时调用,无论以什么形式调用 option() 。如果您要根据多个选项的改变而改变处理器密集型,重载该方法是很有用的。
代码实例: 如果小部件的 _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(); } } |
_show( element, option [, callback ] ) | jQuery (plugin only) | 使用内置的动画方法或使用自定义的特效显示一个元素。如需了解可能的 option 值,请查看 show 。
代码实例: 为自定义动画传递 this._show( this.element, this.options.show, function() { // Focus the element when it's fully visible. this.focus(); } |
_super( [arg ] [, ... ] ) | jQuery (plugin only) | 从父部件中调用相同名称的方法,带有任意指定的参数。本质上是 .call() 。
代码实例: 处理 _setOption: function( key, value ) { if ( key === "title" ) { this.element.find( "h3" ).text( value ); } this._super( key, value ); } |
_superApply( arguments ) | jQuery (plugin only) | 从父部件中调用相同名称的方法,带有参数的数组。本质上是 .apply() 。
代码实例: 处理 _setOption: function( key, value ) { if ( key === "title" ) { this.element.find( "h3" ).text( value ); } this._superApply( arguments ); } |
_trigger( type [, event ] [, data ] ) | Boolean | 触发一个事件及其相关的回调。带有该名称的选项与作为回调被调用的类型相等。 事件名称是小部件名称和类型的小写字母串。 注释:当提供数据时,您必须提供所有三个参数。如果没有传递事件,则传递 如果默认行为是阻止的,则返回
代码实例: 当按下一个键时,触发 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() }); } }); |
destroy() | jQuery (plugin only) | 完全移除小部件功能。这会把元素返回到它的预初始化状态。
代码实例: 当点击小部件的任意锚点时销毁小部件。 this._on( this.element, { "click a": function( event ) { event.preventDefault(); this.destroy(); } }); |
disable() | jQuery (plugin only) | 禁用小部件。
代码实例: 当点击小部件的任意锚点时禁用小部件。 this._on( this.element, { "click a": function( event ) { event.preventDefault(); this.disable(); } }); |
enable() | jQuery (plugin only) | 启用小部件。
代码实例: 当点击小部件的任意锚点时启用小部件。 this._on( this.element, { "click a": function( event ) { event.preventDefault(); this.enable(); } }); |
option( optionName ) | Object | 获取当前与指定的 optionName 关联的值。
代码实例: 获得 this.option( "width" ); |
option() | PlainObject | 获取一个包含键/值对的对象,键/值对表示当前小部件选项哈希。
代码实例: 记录每个小部件选项的键/值对,用于调试。 var options = this.option(); for ( var key in options ) { console.log( key, options[ key ] ); } |
option( optionName, value ) | jQuery (plugin only) | 设置与指定的 optionName 关联的小部件选项的值。
代码实例: 设置 this.option( "width", 500 ); |
option( options ) | jQuery (plugin only) | 为小部件设置一个或多个选项。
代码实例: 设置 this.option({ width: 500, height: 500 }); |
widget() | jQuery | 返回一个包含原始元素或其他相关的生成元素的 jQuery 对象。
代码实例: 当创建小部件时,在小部件的原始元素周围放置一个红色的边框。 _create: function() { this.widget().css( "border", "2px solid red" ); } |
事件 | 类型 | 描述 |
---|---|---|
create( event, ui ) | widgetcreate | 当小部件被创建时触发。
注意: 代码实例: 初始化带有指定 create 回调的小部件: $( ".selector" ).widget({ create: function( event, ui ) {} }); 绑定一个事件监听器到 widgetcreate 事件: $( ".selector" ).on( "widgetcreate", function( event, ui ) {} ); |