Permalink
Browse files

Adding iff.

  • Loading branch information...
1 parent ef8c623 commit 55a6d61227c9a9c55deefb264029fb58365c477b @cohitre committed Jan 3, 2011
Showing with 19 additions and 0 deletions.
  1. +19 −0 jquery.cohitre.js
View
19 jquery.cohitre.js
@@ -1,6 +1,8 @@
(function ($) {
/*
+ * Calls the callback on a jQuery object when
+ * the page has loaded.
* $.initialize(selector, callback, args...);
*/
$.initialize = function (selector) {
@@ -11,12 +13,29 @@ $.initialize = function (selector) {
};
/*
+ * An easy way to create jQuery plugins without polluting
+ * the jQuery namespace.
* $(selector).doOnce(someFunction, arg1, arg2, ....);
*/
$.fn.doOnce = function (callback) {
var args = $.makeArray(arguments).slice(1);
this.length && callback.apply(this, args);
return this;
};
+
+/*
+ * Empties the collection if the condition is not true.
+ * An alternative to conditionals.
+ * $(selector).iff(a === 2)....;
+ * $(selector).iff(function () {
+ * return this.is(".happy");
+ * }).....;
+ */
+$.fn.iff = function (condition) {
+ if ($.isFunction(condition)) {
+ condition = condition.apply(this, $.makeArray(arguments).slice(1));
+ }
+ return this.pushStack(condition ? this : []);
+};
})(jQuery);

0 comments on commit 55a6d61

Please sign in to comment.