### 返回值:jQueryjQuery.callbacks(flags)
### 概述
一個多用途的回調列表對象,提供了強大的的方式來管理回調函數列表。
$.Callbacks()的內部提供了jQuery的$.ajax()?和?$.Deferred()?基本功能組件。它可以用來作為類似基礎定義的新組件的功能。
$.Callbacks()?支持的方法,包括?[callbacks.add()](http://www.css88.com/callbacks.add/),[callbacks.remove()](http://www.css88.com/callbacks.remove/),?[callbacks.fire()](http://www.css88.com/callbacks.fire/)?and?[callbacks.disable()](http://www.css88.com/callbacks.disable/).
### 參數
#### **flags***V1.7*
一個用空格標記分隔的標志可選列表,用來改變回調列表中的行為
### 示例
#### 入門描述:
以下是兩個樣品的方法命名fn1?and?fn2:
##### jQuery 代碼:
~~~
function fn1( value ){
console.log( value );
}
function fn2( value ){
fn1("fn2 says:" + value);
return false;
}
~~~
這些可以添加為回調函數作為一個$.Callbacks的列表,并調用如下:
~~~
var callbacks = $.Callbacks();
callbacks.add( fn1 );
callbacks.fire( "foo!" ); // outputs: foo!
callbacks.add( fn2 );
callbacks.fire( "bar!" ); // outputs: bar!, fn2 says: bar!
~~~
這樣做的結果是,它使構造復雜的回調列表變得簡單,輸入值可以通過盡可能多的函數根據需要輕松使用。
用于以上的兩個具體的方法:?.add()?和?.fire()?.add() 支持添加新的回調回調列表, 而.fire()?提供了一種用于處理在同一列表中的回調方法的途徑.
另一種方法由$.Callbacks?的remove(),用于從回調列表中刪除一個特定的回調。下面是.remove() 使用的一個例子:
~~~
var callbacks = $.Callbacks();
callbacks.add( fn1 );
callbacks.fire( "foo!" ); // outputs: foo!
callbacks.add( fn2 );
callbacks.fire( "bar!" ); // outputs: bar!, fn2 says: bar!
callbacks.remove(fn2);
callbacks.fire( "foobar" );
// only outputs foobar, as fn2 has been removed.
~~~
#### 支持的 Flags描述:
這個 flags 參數是$.Callbacks()的一個可選參數, 結構為一個用空格標記分隔的標志可選列表,用來改變回調列表中的行為 (比如. $.Callbacks( 'unique stopOnFalse' )).
?
**可用的 flags: **
- once: 確保這個回調列表只執行一次(像一個遞延 Deferred).
- memory: 保持以前的值和將添加到這個列表的后面的最新的值立即執行調用任何回調 (像一個遞延 Deferred).
- unique: 確保一次只能添加一個回調(所以有沒有在列表中的重復).
- stopOnFalse: 當一個回調返回false 時中斷調用
默認情況下,回調列表將像事件的回調列表中可以多次觸發。
如何在理想情況下應該使用的flags的例子,見下文:
?
**$.Callbacks( 'once' ):**
~~~
var callbacks = $.Callbacks( "once" );
callbacks.add( fn1 );
callbacks.fire( "foo" );
callbacks.add( fn2 );
callbacks.fire( "bar" );
callbacks.remove( fn2 );
callbacks.fire( "foobar" );
/*
output:
foo
*/
~~~
?
**$.Callbacks( 'memory' ):**
~~~
var callbacks = $.Callbacks( "memory" );
callbacks.add( fn1 );
callbacks.fire( "foo" );
callbacks.add( fn2 );
callbacks.fire( "bar" );
callbacks.remove( fn2 );
callbacks.fire( "foobar" );
/*
output:
foo
fn2 says:foo
bar
fn2 says:bar
foobar
*/
~~~
?
**$.Callbacks( 'unique' ):**
~~~
var callbacks = $.Callbacks( "unique" );
callbacks.add( fn1 );
callbacks.fire( "foo" );
callbacks.add( fn1 ); // repeat addition
callbacks.add( fn2 );
callbacks.fire( "bar" );
callbacks.remove( fn2 );
callbacks.fire( "foobar" );
/*
output:
foo
bar
fn2 says:bar
foobar
*//code>
~~~
?
**$.Callbacks( 'stopOnFalse' ):**
~~~
function fn1( value ){
console.log( value );
return false;
}
function fn2( value ){
fn1("fn2 says:" + value);
return false;
}
var callbacks = $.Callbacks( "stopOnFalse");
callbacks.add( fn1 );
callbacks.fire( "foo" );
callbacks.add( fn2 );
callbacks.fire( "bar" );
callbacks.remove( fn2 );
callbacks.fire( "foobar" );
/*
output:
foo
bar
foobar
*/
~~~
因為$.Callbacks() 支持一個列表的flags而不僅僅是一個,設置幾個flags,有一個累積效應,類似“&&”。這意味著它可能結合創建回調名單,*unique*?和*確保如果名單已經觸發,將有更多的回調調用最新的觸發值*?(i.e.$.Callbacks("unique memory")).
?
**$.Callbacks( 'unique memory' ):**
~~~
function fn1( value ){
console.log( value );
return false;
}
function fn2( value ){
fn1("fn2 says:" + value);
return false;
}
var callbacks = $.Callbacks( "unique memory" );
callbacks.add( fn1 );
callbacks.fire( "foo" );
callbacks.add( fn1 ); // repeat addition
callbacks.add( fn2 );
callbacks.fire( "bar" );
callbacks.add( fn2 );
callbacks.fire( "baz" );
callbacks.remove( fn2 );
callbacks.fire( "foobar" );
/*
output:
foo
fn2 says:foo
bar
fn2 says:bar
baz
fn2 says:baz
foobar
*/
~~~
Flag結合體是使用的$.Callbacks()內部的.done()?和?.fail()一個遞延容器-它們都使用?$.Callbacks('memory once').
$.Callbacks 方法也可以被分離, 為方便起見應該有一個需要定義簡寫版本:
~~~
var callbacks = $.Callbacks(),
add = callbacks.add,
remove = callbacks.remove,
fire = callbacks.fire;
add( fn1 );
fire( "hello world");
remove( fn1 );
~~~
#### $.Callbacks, $.Deferred and Pub/Sub
pub / sub( Observer模式)背后的一般思路 是促進應用程序的松散耦合。而比對其他對象的方法調用的單個對象,一個對象,而不是另一個對象的一個特定的任務或活動,并通知當它發生。觀察家也被稱為訂閱者,我們指的出版商(或主體)觀察對象。出版商事件發生時通知用戶
作為一個組件$.Callbacks()創造能力,它可以實現一個pub / sub系統只使用回調列表。使用$.Callbacks作為主題隊列,發布和訂閱的主題系統可以實現如下:
~~~
var topics = {};
jQuery.Topic = function( id ) {
var callbacks,
method,
topic = id && topics[ id ];
if ( !topic ) {
callbacks = jQuery.Callbacks();
topic = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove
};
if ( id ) {
topics[ id ] = topic;
}
}
return topic;
};
~~~
然后,可以很容易的使用這部分應用程序的發布和訂閱感興趣的事件:
~~~
// Subscribers
$.Topic( "mailArrived" ).subscribe( fn1 );
$.Topic( "mailArrived" ).subscribe( fn2 );
$.Topic( "mailSent" ).subscribe( fn1 );
// Publisher
$.Topic( "mailArrived" ).publish( "hello world!" );
$.Topic( "mailSent" ).publish( "woo! mail!" );
// Here, "hello world!" gets pushed to fn1 and fn2
// when the "mailArrived" notification is published
// with "woo! mail!" also being pushed to fn1 when
// the "mailSent" notification is published.
/*
output:
hello world!
fn2 says: hello world!
woo! mail!
*/
~~~
雖然這是有用的,可以采取進一步的實施。使用$.Deferreds,這是可能的,以確保發表者只為用戶發布一次特別的任務已經完成(解決)通知。這可能是如何在實踐中使用的一些進一步的評論,請參見下面的代碼示例:
~~~
// subscribe to the mailArrived notification
$.Topic( "mailArrived" ).subscribe( fn1 );
// create a new instance of Deferreds
var dfd = $.Deferred();
// define a new topic (without directly publishing)
var topic = $.Topic( "mailArrived" );
// when the deferred has been resolved, publish a
// notification to subscribers
dfd.done( topic.publish );
// Here the Deferred is being resolved with a message
// that will be passed back to subscribers. It's possible to
// easily integrate this into a more complex routine
// (eg. waiting on an ajax call to complete) so that
// messages are only published once the task has actually
// finished.
dfd.resolve( "its been published!" );
~~~
- 速查表
- 核心
- jQuery(selector,[context])
- jQuery(html,[ownerDocument])
- jQuery(callback)
- jQuery.holdReady(hold)
- each(callback)
- size()
- length
- selector
- context
- get([index])
- index([selector|element])
- data([key],[value])
- removeData([name|list])
- jQuery.data(element,[key],[value])
- queue(element,[queueName])
- dequeue([queueName])
- clearQueue([queueName])
- jQuery.noConflict([extreme])
- 選擇器
- #id
- element
- .class
- *
- selector1,selector2,selectorN
- ancestor descendant
- parent > child
- prev + next
- prev ~ siblings
- :first
- :last
- :not(selector)
- :even
- :odd
- :eq(index)
- :gt(index)
- :lt(index)
- :header
- :animated
- :focus
- :contains(text)
- :empty
- :has(selector)
- :parent
- :hidden
- :visible
- [attribute]
- [attribute=value]
- [attribute!=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- [selector1][selector2][selectorN]
- :nth-child
- :first-child
- :last-child
- :only-child
- :input
- :text
- :password
- :radio
- :checkbox
- :submit
- :image
- :reset
- :button
- :file
- :enabled
- :disabled
- :checked
- :selected
- 屬性
- attr(name|pro|key,val|fn)
- removeAttr(name)
- prop(name|pro|key,val|fn)
- removeProp(name)
- addClass(class|fn)
- removeClass([class|fn])
- toggleClass(class|fn[,sw])
- html([val|fn])
- text([val|fn])
- val([val|fn|arr])
- 篩選
- eq(index|-index)
- first()
- last()
- hasClass(class)
- filter(expr|obj|ele|fn)
- is(expr|obj|ele|fn)
- map(callback)
- has(expr|ele)
- not(expr|ele|fn)
- slice(start, [end])
- children([expr])
- closest(expr,[con]|obj|ele)
- find(expr|obj|ele)
- next([expr])
- nextAll([expr])
- nextUntil([exp|ele][,fil])
- parent([expr])
- parents([expr])
- parentsUntil([exp|ele][,fil])
- prev([expr])
- prevAll([expr])
- prevUntil([exp|ele][,fil])
- siblings([expr])
- add(expr|ele|html|obj[,con])
- andSelf()
- contents()
- end()
- 文檔處理
- append(content|fn)
- appendTo(content)
- prepend(content|fn)
- prependTo(content)
- after(content|fn)
- before(content|fn)
- insertAfter(content)
- insertBefore(content)
- wrap(html|ele|fn)
- unwrap()
- wrapAll(html|ele)
- wrapInner(html|ele|fn)
- replaceWith(content|fn)
- replaceAll(selector)
- empty()
- remove([expr])
- detach([expr])
- clone([Even[,deepEven]])
- CSS
- css(name|pro|[,val|fn])
- offset([coordinates])
- position()
- scrollTop([val])
- scrollLeft([val])
- height([val|fn])
- width([val|fn])
- innerHeight()
- innerWidth()
- outerHeight([options])
- outerWidth([options])
- 事件
- ready(fn)
- on(events,[selector],[data],fn)
- off(events,[selector],[fn])
- bind(type,[data],fn)
- one(type,[data],fn)
- trigger(type,[data])
- triggerHandler(type,[data])
- unbind(type,[data|fn])
- live(type,[data],fn)
- die(type,[fn])
- delegate(sel,[type],[data],fn)
- undelegate([sel,[type],fn])
- hover([over,]out)
- toggle(fn, fn2, [fn3, fn4, ...])
- blur([[data],fn])
- change([[data],fn])
- click([[data],fn])
- dblclick([[data],fn])
- error([[data],fn])
- focus([[data],fn])
- focusout([data],fn)
- keydown([[data],fn])
- keypress([[data],fn])
- keyup([[data],fn])
- mousedown([[data],fn])
- mouseenter([[data],fn])
- mouseleave([[data],fn])
- mousemove([[data],fn])
- mouseout([[data],fn])
- mouseover([[data],fn])
- mouseup([[data],fn])
- resize([[data],fn])
- scroll([[data],fn])
- select([[data],fn])
- submit([[data],fn])
- unload([[data],fn])
- 效果
- show([speed,[easing],[fn]])
- hide([speed,[easing],[fn]])
- slideDown([speed],[easing],[fn])
- slideUp([speed,[easing],[fn]])
- slideToggle([speed],[easing],[fn])
- fadeIn([speed],[easing],[fn])
- fadeOut([speed],[easing],[fn])
- fadeTo([[speed],opacity,[easing],[fn]])
- fadeToggle([speed,[easing],[fn]])
- animate(param,[spe],[e],[fn])
- stop([cle],[jum])
- delay(duration,[queueName])
- jQuery.fx.off
- jQuery.fx.interval
- Ajax
- jQuery.ajax(url,[settings])
- load(url,[data],[callback])
- jQuery.get(url,[data],[callback],[type])
- jQuery.getJSON(url,[data],[callback])
- jQuery.getScript(url,[callback])
- jQuery.post(url,[data],[callback],[type])
- ajaxComplete(callback)
- ajaxError(callback)
- ajaxSend(callback)
- ajaxStart(callback)
- ajaxStop(callback)
- ajaxSuccess(callback)
- jQuery.ajaxSetup([options])
- serialize()
- serializeArray()
- 工具
- jQuery.support
- jQuery.browser
- jQuery.browser.version
- jQuery.boxModel
- jQuery.each(object,[callback])
- jQuery.extend([deep],target,object1,[objectN])
- jQuery.grep(array,callback,[invert])
- jQuery.makeArray(obj)
- jQuery.map(array, callback)
- jQuery.inArray(val,arr,[from])
- jQuery.toArray()
- jQuery.sub()
- jQuery.when(deferreds)
- jQuery.merge(first,second)
- jQuery.unique(array)
- jQuery.parseJSON(json)
- jQuery.noop
- jQuery.proxy(function,context)
- jQuery.contains(container, contained)
- jQuery.isArray(obj)
- jQuery.isFunction(obj)
- jQuery.isEmptyObject(obj)
- jQuery.isPlainObject(obj)
- jQuery.isWindow(obj)
- jQuery.isNumeric(value)
- jQuery.type(obj)
- jQuery.trim(str)
- jQuery.param(obj,[traditional])
- jQuery.error(message)
- Deferred
- def.done(donCal,[donCal])
- def.fail(failCal)
- def.isRejected()
- def.isResolved()
- def.reject(args)
- def.rejectWith(context,[args])
- def.resolve(args)
- def.resolveWith(context,args)
- def.then(doneCal,failCal)
- def.progress([type],[target])
- def.pipe([donFil],[faiFil],[proFil])
- def.always(alwCal,[alwCal])
- def.notify(args)
- def.notifyWith(context,[args])
- def.state()
- Callbacks
- callbacks.add(callbacks)
- callbacks.disable()
- callbacks.empty()
- callbacks.fire(arguments)
- callbacks.fired()
- callbacks.fireWith([context][,args])
- callbacks.has(callback)
- callbacks.lock()
- callbacks.locked()
- callbacks.remove(callbacks)
- jQuery.callbacks(flags)
- 關于
- 關于jQuery API 文檔
- 提交bug及獲取更新
- 其它
- HTML5速查表
- 正則表達式速查表