Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Created August 20, 2012 08:39
Show Gist options
  • Save bjartwolf/3402325 to your computer and use it in GitHub Desktop.
Save bjartwolf/3402325 to your computer and use it in GitHub Desktop.
refactor underscore
// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
var self = this;
// Non-editable catalog data - would come from the server
self.availableMeals = [
{ mealName: "Standard (sandwich)", price: 0 },
{ mealName: "Premium (lobster)", price: 34.95 },
{ mealName: "Ultimate (whole zebra)", price: 290 }
];
// Editable data
self.seats = ko.observableArray([
new SeatReservation("Steve", _.first(self.availableMeals)),
new SeatReservation("Bert", _.first(self.availableMeals))
]);
// Computed data
self.totalSurcharge = ko.computed(function () {
var sum = _.reduce(self.seats(), function (sum, reservation) {
return sum + reservation.meal().price;
}, 0);
return sum;
});
// Operations
self.addSeat = function () {
self.seats.push(new SeatReservation("", _.first(self.availableMeals)));
};
self.removeSeat = function (seat) {
self.seats.remove(seat);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment