Created
August 20, 2012 08:39
-
-
Save bjartwolf/3402325 to your computer and use it in GitHub Desktop.
refactor underscore
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
// 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