|
6 | 6 | <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script> |
7 | 7 | <script type="text/javascript" src="jquery.livelink.js"></script> |
8 | 8 | <script type="text/javascript" src="jquery.tmpl.js"></script> |
9 | | -<script type="text/javascript"> |
10 | | -jQuery(function(){ |
11 | | - |
12 | | -// define some basic default data to start with |
13 | | -var contacts = [ |
14 | | - { firstName: "Dave", lastName: "Reed", phones: [ |
15 | | - { type: "Mobile", number: "(555) 121-2121" }, |
16 | | - { type: "Home", number: "(555) 123-4567" } ] }, |
17 | | - { firstName: "Joe", lastName: "Smith", phones: [ |
18 | | - { type: "Mobile", number: "(555) 444-2222" }, |
19 | | - { type: "Home", number: "(555) 999-1212" } ] } |
20 | | -]; |
21 | | - |
22 | | -// enable using 'contacts' as the name of the template |
23 | | -$.templates.contacts = $.tmpl($("#contacttmpl").html()); |
24 | | - |
25 | | -$.extend($.convertFn, { |
26 | | - // linking converter that normalizes phone numbers |
27 | | - phone: function(value) { |
28 | | - // turn a string phone number into a normalized one with dashes |
29 | | - // and parens |
30 | | - value = parseInt(value.replace(/[\(\)\- ]/g, ""), 10).toString(); |
31 | | - value = ("0000000000" + value).substr(-10); |
32 | | - value = "(" + value.substr(0,3) + ") " + value.substr(3,3) + "-" + value.substr(6); |
33 | | - return value; |
34 | | - }, |
35 | | - fullname: function(value, settings) { |
36 | | - return settings.source.firstName + " " + settings.source.lastName; |
37 | | - } |
38 | | -}); |
39 | | - |
40 | | -// show the results of the linking -- object graph is already full of the data |
41 | | -$("#save").click(function() { |
42 | | - $("#results").html(JSON.stringify(contacts, null, 4)); |
43 | | -}); |
44 | | - |
45 | | -// add a new contact when clicking the insert button. |
46 | | -// notice that no code here exists that explicitly redraws |
47 | | -// the template. |
48 | | -$("#insert").click(function() { |
49 | | - $.push(contacts, { firstName: "", lastName: "", phones: [] }); |
50 | | -}); |
51 | | - |
52 | | -// function that clears the current template and renders it with the |
53 | | -// current state of the global contacts variable. |
54 | | -function refresh() { |
55 | | - $(".contacts").empty().append("contacts", {contacts:contacts}); |
56 | | - // bind inputs to the data items |
57 | | - $(".contact").each(function(i) { |
58 | | - var contact = contacts[i]; |
59 | | - $(".firstname", this).linkTo("val", contact, "firstName"); |
60 | | - $(".lastname", this).linkTo("val", contact, "lastName"); |
61 | | - $(".contact-fullname", this).linkFrom("text", contact, null, "fullname"); |
62 | | - $(".contact-remove", this).click(function() { |
63 | | - $.splice(contacts, i, 1); |
64 | | - }); |
65 | | - $(".phone", this).each(function(i) { |
66 | | - var phone = contact.phones[i]; |
67 | | - $(".phone-type", this).linkTo("val", phone, "type"); |
68 | | - $(".phone-number", this).linkTo("val", phone, "number", "phone"); |
69 | | - $(".phone-remove", this).click(function() { |
70 | | - // note: I'd like to only redraw the phones portion of the |
71 | | - // template, but jquery.tmpl.js does not support nested templates |
72 | | - // very easily. So here I am triggering an arrayChange event on |
73 | | - // the main contacts array to force the entire thing to refresh. |
74 | | - // Note that user input is not lost since the live linking has |
75 | | - // already stored the values in the object graph. |
76 | | - $.splice(contact.phones, i, 1); |
77 | | - $([contacts]).trigger("arrayChange"); |
78 | | - }); |
79 | | - }); |
80 | | - $(".newphone", this).click(function() { |
81 | | - $.push(contact.phones, { type: "", number: "" }); |
82 | | - $([contacts]).trigger("arrayChange"); |
83 | | - }); |
84 | | - }); |
85 | | -} |
86 | | - |
87 | | -// subscribe to changes to the contact array, automatically refreshing |
88 | | -// the rendering of the template |
89 | | -$([contacts]).arrayChange(refresh); |
90 | | - |
91 | | -// initial view on load |
92 | | -refresh(); |
93 | | - |
94 | | - |
95 | | -}); |
96 | | -</script> |
| 9 | +<script type="text/javascript" src="demo-contacts.js"></script> |
97 | 10 | <script id="contacttmpl" type="text/html"> |
98 | 11 | <tr> |
99 | 12 | <th> </th> |
|
138 | 51 | <table class="contacts"> |
139 | 52 | </table> |
140 | 53 | <input type="button" id="insert" value="Insert new contact" /> |
| 54 | +<input type="button" id="sort" value="Sort by Last Name" /> |
141 | 55 | <input type="button" id="save" value="Save contacts" /> |
142 | 56 |
|
143 | 57 | <pre id="results"> |
|
0 commit comments