-
-
Save Teino1978-Corp/d5787856642da337d3c0 to your computer and use it in GitHub Desktop.
JS Collection Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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