Skip to content

Instantly share code, notes, and snippets.

@romeoh
Created August 28, 2012 23:47
Show Gist options
  • Save romeoh/3505418 to your computer and use it in GitHub Desktop.
Save romeoh/3505418 to your computer and use it in GitHub Desktop.
knockout.js sample coding
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi"/>
<title></title>
<style type="text/css">
</style>
</head>
<body>
<h1>메뉴선택</h1>
메뉴선택: <select data-bind="options: availableMeals,
optionsText: 'mealName',
value: chosenMeal"></select>
<p>
선택한 메뉴:
<b data-bind="text: chosenMeal().description"></b>
(가격: <span data-bind='text: chosenMeal().extraCost'></span>)
</p>
<button id="btnAdd">add</button>
<script type="text/javascript" src="knockout-2.1.0.js"></script>
<script type="text/javascript">
var availableMeals = [
{ mealName: '일반', description: '그냥밥', extraCost: '0원' },
{ mealName: '프리미엄', description: '비싼밥', extraCost: '1,000원' },
{ mealName: '디럭스', description: '엄청 비싼밥', extraCost: '10,000원' }
];
var viewModel = {
chosenMeal: ko.observable(availableMeals[0])
}
ko.applyBindings(viewModel);
btnAdd.addEventListener("click", function(){
var obj = new Object();
obj.mealName = '추가';
obj.description = '싼밥';
obj.extraCost = '20,000원';
availableMeals[3] = obj;
console.log(JSON.stringify(availableMeals))
ko.applyBindings(viewModel);
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment