Created
August 23, 2011 19:57
-
-
Save kingbuzzman/1166328 to your computer and use it in GitHub Desktop.
Big guns.. knockout has arrived
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Example 3.a</title> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js" type="text/javascript" charset="ISO-8859-1"></script> | |
| <script src="https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.min.js" type="text/javascript"></script> | |
| <script src="http://github.com/downloads/SteveSanderson/knockout/knockout-1.2.1.js" type="text/javascript" charset="ISO-8859-1"></script> | |
| <script type="text/javascript"> | |
| $(document).ready(function(){ | |
| var viewController = { | |
| name: ko.observable(), | |
| phone: ko.observable(), | |
| selected: ko.observableArray() | |
| }; | |
| viewController.list = ko.dependentObservable(function(){ | |
| var itemLabels = $("#label_items"); | |
| var items = []; | |
| // adds binding (hack) | |
| if(viewController.selected().length == 0) return items; | |
| $("#items").children().map(function(index,item){ | |
| item = $(item); | |
| if(item.attr("selected")){ | |
| items.push(item.html()); | |
| } | |
| }); | |
| return items; | |
| }); | |
| viewController.total = ko.dependentObservable(function(){ | |
| if(viewController.selected().length == 0) return 0; | |
| return viewController.selected().reduce(function(a, b){ return parseFloat(a) + parseFloat(b); }); | |
| }); | |
| ko.applyBindings(viewController); | |
| }); | |
| </script> | |
| <script type="text/html" id="newsButton"> | |
| <li>${ this.data }</li> | |
| </script> | |
| </head> | |
| <body> | |
| <div> | |
| <label for="name">Name: </label> | |
| <input name="name" id="name" type="text" data-bind="value: name, valueUpdate: 'keyup'"><br> | |
| <label for="phone">Phone: </label> | |
| <input name="phone" id="phone" type="text" data-bind="value: phone, valueUpdate: 'keyup'"><br> | |
| <label for="items">Items<label> | |
| <select name="items" id="items" multiple="multiple" size="5" data-bind="selectedOptions: selected"> | |
| <option value="5.00">$5.00 Cheese burger</option> | |
| <option value="7.00">$7.00 1lb Cheese burger</option> | |
| <option value="3.00">$3.00 Fries</option> | |
| <option value="2.00">$2.00 Soda</option> | |
| <option value="8.00">$8.00 Cheese burger combo</option> | |
| </select> | |
| </div> | |
| <br><br> | |
| Checkout for <strong data-bind="text: name"></strong><br> | |
| Call: <strong data-bind="text: phone"></strong><br> | |
| Order:<br> | |
| <ul id="label_items" data-bind="template: { name: 'newsButton', foreach: list }"></ul><br> | |
| Total $: <strong data-bind="text: total"></strong> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment