Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeckdude/a16968b2dc2734e459dc to your computer and use it in GitHub Desktop.
Save zeckdude/a16968b2dc2734e459dc to your computer and use it in GitHub Desktop.
Backbone from Scratch Chapter Project: Collections
// BACKBONE COLLECTIONS PROJECT
console.log("Begin - BACKBONE COLLECTIONS PROJECT");
// Create a Backbone collection for the Vehicle model you created in the last project.
var Cars = Backbone.Collection.extend({
model: Vehicle
});
// Add the following cars inside the collection:
// * car1: { registrationNumber = “XLI887”, colour = “Blue” }
// * car2: { registrationNumber = “ZNP123”, colour = “Blue” }
// * car3: { registrationNumber = “XUV456”, colour = “Gray” }
var cars = new Cars([
new Car({ registrationNumber: "XLI887", color: "Blue" }),
new Car({ registrationNumber: "ZNP123", color: "Blue" }),
new Car({ registrationNumber: "XUV456", color: "Gray" })
]);
// Find all the Blue colours and log them in the console.
console.log(cars.where({ color: "Blue" }));
// Find the car with the registration number XLI887 and log it in the console.
var carWithSpecifiedRegistrationNumber = cars.findWhere({ registrationNumber: "XLI887" });
console.log(carWithSpecifiedRegistrationNumber);
// Remove this car from the collection.
cars.remove(carWithSpecifiedRegistrationNumber);
// Convert the collection to a JSON object and log it in the console.
console.log(cars.toJSON());
// Iterate the collection and log each car in the console.
cars.each(function(car) {
console.log(car);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment