Skip to content

Instantly share code, notes, and snippets.

@Teino1978-Corp
Forked from agustinhaller/collection_helper.js
Created October 31, 2015 08:06
Show Gist options
  • Save Teino1978-Corp/d5787856642da337d3c0 to your computer and use it in GitHub Desktop.
Save Teino1978-Corp/d5787856642da337d3c0 to your computer and use it in GitHub Desktop.
JS Collection Helper
var collection_helper = (function () {
var collection = [],
$collection = $(collection),
inCollection = function (item) {
//debugger;
var index = -1,
iterator = 0,
total_iterator = $collection.length;
for (iterator; iterator < total_iterator; iterator++) {
if ($collection[iterator].id == item.id) {
index = iterator;
}
}
return index;
},
addToCollection = function (item) {
var indexOfItem = inCollection(item);
if (indexOfItem == -1) {
$collection.push(item);
}
},
removeFromCollection = function (item) {
var indexOfItem = inCollection(item);
if (indexOfItem != -1) {
$collection.splice(indexOfItem, 1);
}
},
clear = function () {
collection = [];
$collection = $(collection);
},
getCollection = function () {
return $collection;
};
return {
inCollection: function (item) {
var in_collection = (inCollection(item) != -1) ? true : false;
return in_collection;
},
addToCollection: function (item) {
addToCollection(item);
},
removeFromCollection: function (item) {
removeFromCollection(item);
},
clear: function () {
clear();
},
getCollection: function () {
var collection = getCollection();
return collection;
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment