Skip to content

Instantly share code, notes, and snippets.

@piyushchauhan2011
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save piyushchauhan2011/76100ef2d3372cf0613d to your computer and use it in GitHub Desktop.

Select an option

Save piyushchauhan2011/76100ef2d3372cf0613d to your computer and use it in GitHub Desktop.
Testing Knockout.js// source http://jsbin.com/weruw/1
<html>
<head>
<meta charset="UTF-8">
<title>Testing Knockout.js</title>
</head>
<body>
Choose a ticket class:
<select data-bind="options: tickets,
optionsCaption: 'Choose...',
optionsText: 'name',
value: chosenTicket"></select>
<button data-bind="enable: chosenTicket,
click: resetTicket">Clear</button>
<p data-bind="with: chosenTicket">
You have chosen <b data-bind="text: name"></b>
($<span data-bind="text: price"></span>)
</p>
<p>
First Name: <span data-bind="text: firstName"></span>
<br>
Last Name: <span data-bind="text: lastName"></span>
<br>
Full Name: <span data-bind="text: fullName"></span>
<br>
<input data-bind="textInput: firstName" placeholder="first name"/>
<input data-bind="textInput: lastName" placeholder="last name"/>
</p>
</body>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script>
function TicketsViewModel() {
this.tickets = [
{ name: 'Economy', price: 199.95 },
{ name: 'Business', price: 449.22 },
{ name: 'First Class', price: 1199.99 }
];
this.chosenTicket = ko.observable();
this.resetTicket = function() { this.chosenTicket(null); };
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
}
ko.applyBindings(new TicketsViewModel());
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment