Merge pull request #1725 from johnnyreilly/master

jQuery: unbind and undelegate JSDoc
This commit is contained in:
John Reilly
2014-02-24 16:04:32 +00:00
2 changed files with 153 additions and 5 deletions

View File

@@ -590,6 +590,70 @@ function test_bind() {
});
}
function test_unbind() {
$("#foo").unbind();
$("#foo").unbind("click");
var handler = function () {
alert("The quick brown fox jumps over the lazy dog.");
};
$("#foo").bind("click", handler);
$("#foo").unbind("click", handler);
$("#foo").bind("click", function () {
alert("The quick brown fox jumps over the lazy dog.");
});
// Will NOT work
$("#foo").unbind("click", function () {
alert("The quick brown fox jumps over the lazy dog.");
});
$("#foo").bind("click.myEvents", handler);
$("#foo").unbind("click");
$("#foo").unbind("click.myEvents");
$("#foo").unbind(".myEvents");
var timesClicked = 0;
$("#foo").bind("click", function (event) {
alert("The quick brown fox jumps over the lazy dog.");
timesClicked++;
if (timesClicked >= 3) {
$(this).unbind(event);
}
});
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("#theone")
.bind("click", aClick)
.text("Can Click!");
});
$("#unbind").click(function () {
$("#theone")
.unbind("click", aClick)
.text("Does nothing...");
});
$("p").unbind();
$("p").unbind("click");
var foo = function () {
// Code to handle some kind of event
};
$("p").bind("click", foo); // ... Now foo will be called when paragraphs are clicked ...
$("p").unbind("click", foo); // ... foo will no longer be called.
}
function test_blur() {
$('#target').blur(function () {
alert('Handler for .blur() called.');
@@ -1119,7 +1183,6 @@ function test_delay() {
});
}
/* Not existing, but not recommended either
function test_delegate() {
$("table").delegate("td", "click", function () {
$(this).toggleClass("chosen");
@@ -1137,7 +1200,7 @@ function test_delegate() {
$("body").delegate("a", "click", function (event) {
event.preventDefault();
});
$("body").delegate("p", "myCustomEvent", function (e, myName, myValue) {
$("body").delegate("p", "myCustomEvent", function (e, myName?, myValue?) {
$(this).text("Hi there!");
$("span").stop().css("opacity", 1)
.text("myName = " + myName)
@@ -1147,7 +1210,48 @@ function test_delegate() {
$("p").trigger("myCustomEvent");
});
}
*/
function test_undelegate() {
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body")
.delegate("#theone", "click", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body")
.undelegate("#theone", "click", aClick)
.find("#theone").text("Does nothing...");
});
$("p").undelegate();
$("p").undelegate("click");
var foo = function () {
// Code to handle some kind of event
};
// ... Now foo will be called when paragraphs are clicked ...
$("body").delegate("p", "click", foo);
// ... foo will no longer be called.
$("body").undelegate("p", "click", foo);
var foo = function () {
// Code to handle some kind of event
};
// Delegate events under the ".whatever" namespace
$("form").delegate(":button", "click.whatever", foo);
$("form").delegate("input[type='text'] ", "keypress.whatever", foo);
// Unbind all events delegated under the ".whatever" namespace
$("form").undelegate(".whatever");
}
function test_dequeue() {
$("button").click(function () {

48
jquery/jquery.d.ts vendored
View File

@@ -2605,15 +2605,59 @@ interface JQuery {
*/
trigger(event: JQueryEventObject, extraParameters?: Object): JQuery;
/**
* Execute all handlers attached to an element for an event.
*
* @param eventType A string containing a JavaScript event type, such as click or submit.
* @param extraParameters An array of additional parameters to pass along to the event handler.
*/
triggerHandler(eventType: string, ...extraParameters: any[]): Object;
/**
* Remove a previously-attached event handler from the elements.
*
* @param eventType A string containing a JavaScript event type, such as click or submit.
* @param handler The function that is to be no longer executed.
*/
unbind(eventType?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery;
/**
* Remove a previously-attached event handler from the elements.
*
* @param eventType A string containing a JavaScript event type, such as click or submit.
* @param fls Unbinds the corresponding 'return false' function that was bound using .bind( eventType, false ).
*/
unbind(eventType: string, fls: boolean): JQuery;
/**
* Remove a previously-attached event handler from the elements.
*
* @param evt A JavaScript event object as passed to an event handler.
*/
unbind(evt: any): JQuery;
/**
* Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements.
*/
undelegate(): JQuery;
undelegate(selector: any, eventType: string, handler?: (eventObject: JQueryEventObject) => any): JQuery;
undelegate(selector: any, events: any): JQuery;
/**
* Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements.
*
* @param selector A selector which will be used to filter the event results.
* @param eventType A string containing a JavaScript event type, such as "click" or "keydown"
* @param handler A function to execute at the time the event is triggered.
*/
undelegate(selector: string, eventType: string, handler?: (eventObject: JQueryEventObject) => any): JQuery;
/**
* Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements.
*
* @param selector A selector which will be used to filter the event results.
* @param events An object of one or more event types and previously bound functions to unbind from them.
*/
undelegate(selector: string, events: Object): JQuery;
/**
* Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements.
*
* @param namespace A string containing a namespace to unbind all events from.
*/
undelegate(namespace: string): JQuery;
unload(handler: (eventObject: JQueryEventObject) => any): JQuery;