Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created May 31, 2011 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/1000934 to your computer and use it in GitHub Desktop.
Save cowboy/1000934 to your computer and use it in GitHub Desktop.
.pushStack idea
// 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);
};
});
@cowboy
Copy link
Author

cowboy commented May 31, 2011

Note: completely untested.

Also, perhaps the callback to $.each in the third example could be a reusable $.pushStackify method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment