Skip to content

Instantly share code, notes, and snippets.

@jgable
Created April 20, 2011 14:06
Show Gist options
  • Save jgable/931412 to your computer and use it in GitHub Desktop.
Save jgable/931412 to your computer and use it in GitHub Desktop.
jQuery-linqHelpers - Helper functions from linq.
/* Author:
Jacob Gable, http://jacob4u2.posterous.com
License:
MS-PL
*/
jQuery.any = function (collection, compareFunc) {
if (!$.isArray(collection) || !$.isFunction(compareFunc)) {
return false;
}
for (var i = 0; i < collection.length; i++) {
if (compareFunc(collection[i], i)) {
return true;
}
}
return false;
};
jQuery.first = function (collection, compareFunc) {
if (!$.isArray(collection) || !$.isFunction(compareFunc)) {
return null;
}
for (var i = 0; i < collection.length; i++) {
if (compareFunc(collection[i], i)) {
return collection[i];
}
}
return null;
};
// Returns a flat list of array elements (concat'ed together)
jQuery.mapMany = function (collection, aggregateFunc) {
if (!$.isArray(collection) || !$.isFunction(aggregateFunc)) {
return [];
}
var many = [];
$.each($.map(collection, aggregateFunc), function (i, eachEl) {
many = many.concat(eachEl);
});
return many;
};
jQuery.postJson = function (url, data, successCallback, errorCallback) {
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successCallback,
error: errorCallback
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment