JS Collections
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
JavaScript
$j = jQuery.noConflict();
function ObjectCollection() { }
ObjectCollection.prototype = [];
ObjectCollection.prototype.fetch = function(callback) {
var parent = this;
$j.getJSON("https://demo9071224.mockable.io/objects.json", function(data) {
$j.each(data.objects, function(i, customObject) {
var myCustomObject = new CustomObject(customObject);
parent.push(myCustomObject);
});
callback();
});
};
function CustomObject(name) {
this.name = name;
}
function doSomethingWithCollection(objectCollection) {
this.objectCollection = objectCollection;
var that = this;
this.objectCollection.fetch(function () {
console.log(that.objectCollection);
console.log(that.objectCollection.length);
that.objectCollection.forEach(function(object) {
console.log(object);
});
});
}
var collection = new ObjectCollection;
doSomethingWithCollection(collection);