Created
July 8, 2014 21:12
-
-
Save hotcoder/0bbf8a8504e24ccd6973 to your computer and use it in GitHub Desktop.
seat reservation
This file contains 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
// Class to represent a row in the seat reservations grid | |
function SeatReservation(name, initialMeal) { | |
var self = this; | |
self.name = name; | |
self.meal = ko.observable(initialMeal); | |
self.namesLists = ko.observableArray([{ | |
Name: "Vegetarian Raw Meal" | |
}, { | |
Name: "Vegetarian Vegan Meal" | |
}, { | |
Name: "Fruit Platter Meal" | |
}]); | |
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 Meals data - would come from the server | |
self.availableMeals = [{ | |
mealName: "Vegetarian Raw Meal", | |
price: 10.52 | |
}, { | |
mealName: "Vegetarian Vegan Meal", | |
price: 34.95 | |
}, { | |
mealName: "Fruit Platter Meal", | |
price: 45.50 | |
}]; | |
self.addNewName = function(parent, data) { | |
//console.log(parent); | |
parent.namesLists().push({ | |
Name: "Vegetarian Meal" | |
}); | |
console.log(parent.name); | |
var targetSeat = ko.utils.arrayFirst(self.seats(), function(currentSeat) { | |
return currentSeat.name === parent.name; | |
}); | |
console.log(targetSeat.name); | |
if (targetSeat) { | |
targetSeat.namesLists(parent.namesLists()); | |
} | |
// self.seats.push(); | |
//console.log(parent.namesLists()); | |
} | |
// Editable data - seats Array | |
self.seats = ko.observableArray([ | |
new SeatReservation("Sampath", self.availableMeals[0]), | |
new SeatReservation("Lokuge", self.availableMeals[1]) | |
]); | |
// Computed Total amount | |
self.totalAmount = ko.computed(function() { | |
var total = 0; | |
for (var i = 0; i < self.seats().length; i++) | |
total += self.seats()[i].meal().price; | |
return total; | |
}); | |
// add seats | |
self.addSeat = function() { | |
self.seats.push(new SeatReservation("Chaminda", self.availableMeals[2])); | |
}; | |
// remove seats | |
self.removeSeat = function(seat) { | |
self.seats.remove(seat); | |
}; | |
} | |
$(document).ready(function() { | |
ko.applyBindings(new ReservationsViewModel()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment