Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.