Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Higgins
committed
Jun 8, 2010
1 parent
afd7269
commit f9851e9
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
jQuery.hitch() - Advanced scope manipulation for jQuery | ||
version: 0.1, Peter Higgins (dante at dojotoolkit.org) | ||
(c) 2004-2010 The Dojo Foundation - adapted from `dojo.hitch` | ||
Either AFL/New BSD license, see: http://dojotoolkit.org/license | ||
usage 1: | ||
var obj = { | ||
attr: "blah", | ||
func: function(){ | ||
console.log(this.attr); | ||
} | ||
} | ||
// most useful example out of context, with setTimeout/Interval: | ||
setInterval($.hitch(obj, "func"), 2100) // call obj.func() in scope of obj | ||
setTimeout($.hitch(obj, function(){ | ||
this.attr = "blargh"; | ||
this.func(); | ||
}), 2100); | ||
usage 2: | ||
// pseudo-class | ||
var Thinger = function(){ | ||
this.foo = "bar"; | ||
this.bar = function(event){ | ||
// super generic function to reuse lots | ||
this.foo = $(event.target).attr("id"); | ||
} | ||
} | ||
var mine = new Thinger(); | ||
var yours = new Thinger(); | ||
$(".foo").click($.hitch(mine, "bar")); | ||
$(mine).bind("bar", $.hitch(yours, "bar")); | ||
*/ | ||
;(function($){ | ||
|
||
$.hitch = function(scope, method){ | ||
// summary: Create a function that will only ever execute in a given scope | ||
if(!method){ method = scope; scope = null; } | ||
if(typeof method == "string"){ | ||
scope = scope || window; | ||
if(!scope[method]){ throw(['method not found']); } | ||
return function(){ return scope[method].apply(scope, arguments || []); }; | ||
} | ||
return !scope ? method : function(){ return method.apply(scope, arguments || []); }; | ||
} | ||
|
||
})(jQuery); | ||
|