Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Created August 19, 2012 21:44
Show Gist options
  • Save bjartwolf/3397909 to your computer and use it in GitHub Desktop.
Save bjartwolf/3397909 to your computer and use it in GitHub Desktop.
buster 301 src
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
self.formattedPrice = ko.computed(function () {
var price = self.meal().price;
return price ? "$" + price.toFixed(2) : "None";
});
}
// 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", self.availableMeals[0]),
new SeatReservation("Bert", self.availableMeals[0])
]);
// Computed data
self.totalSurcharge = ko.computed(function () {
var total = 0,
i;
for (i = 0; i < self.seats().length; i += 1) {
total += self.seats()[i].meal().price;
}
return total;
});
// Operations
self.addSeat = function () {
self.seats.push(new SeatReservation("", self.availableMeals[0]));
};
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