From 02348d3eb0d88f81b526e4a20ca358ce49692e0c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 16 Jan 2014 14:00:56 +0000 Subject: [PATCH] jQuery: added proxy / removeData JSDoc + tests --- jquery/jquery-tests.ts | 109 +++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 50 ++++++++++++++++--- 2 files changed, 153 insertions(+), 6 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index e024f1dc6..95fed2635 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -946,6 +946,17 @@ function test_removeData() { $("span:eq(3)").text("" + $("div").data("test2")); } +function test_jQuery_removeData() { + var div = $("div")[0]; + $("span:eq(0)").text("" + $("div").data("test1")); + jQuery.data(div, "test1", "VALUE-1"); + jQuery.data(div, "test2", "VALUE-2"); + $("span:eq(1)").text("" + jQuery.data(div, "test1")); + jQuery.removeData(div, "test1"); + $("span:eq(2)").text("" + jQuery.data(div, "test1")); + $("span:eq(3)").text("" + jQuery.data(div, "test2")); +} + function test_dblclick() { $('#target').dblclick(function () { alert('Handler for .dblclick() called.'); @@ -1731,6 +1742,104 @@ function test_hasData() { $p.append(jQuery.hasData(p) + " "); } +function test_jQuery_proxy() { + + function test1() { + var me = { + type: "zombie", + test: function (event?) { + // Without proxy, `this` would refer to the event target + // use event.target to reference that element. + var element = event.target; + $(element).css("background-color", "red"); + + // With proxy, `this` refers to the me object encapsulating + // this function. + $("#log").append("Hello " + this.type + "
"); + $("#test").off("click", this.test); + } + }; + + var you = { + type: "person", + test: function (event?) { + $("#log").append(this.type + " "); + } + }; + + // Execute you.test() in the context of the `you` object + // no matter where it is called + // i.e. the `this` keyword will refer to `you` + var youClick = $.proxy(you.test, you); + + // attach click handlers to #test + $("#test") + // this === "zombie"; handler unbound after first click + .on("click", $.proxy(me.test, me)) + + // this === "person" + .on("click", youClick) + + // this === "zombie" + .on("click", $.proxy(you.test, me)) + + // this === "