Created
May 31, 2011 17:36
-
-
Save cowboy/1000934 to your computer and use it in GitHub Desktop.
.pushStack idea
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
// currently | |
jQuery.fn.extend({ | |
foo: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "foo", selector); | |
}, | |
bar: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "bar", selector); | |
}, | |
baz: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "baz", selector); | |
} | |
}); | |
// removing the 3rd argument from .pushStack | |
jQuery.fn.extend({ | |
foo: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "foo"); | |
}, | |
bar: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "bar"); | |
}, | |
baz: function(selector) { | |
// create new elems set somehow | |
return this.pushStack(elems, "baz"); | |
} | |
}); | |
// factory that keeps the 2nd .pushStack argument, and makes things smaller | |
$.each({ | |
foo: function(selector) { | |
// create new elems set somehow | |
return elems; | |
}, | |
bar: function(selector) { | |
// create new elems set somehow | |
return elems; | |
}, | |
baz: function(selector) { | |
// create new elems set somehow | |
return elems; | |
} | |
}, function(method, fn) { | |
jQuery.fn[method] = function() { | |
return this.pushStack(fn.apply(this, arguments), method); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: completely untested.
Also, perhaps the callback to
$.each
in the third example could be a reusable$.pushStackify
method.