1- jQuery ( function ( $ ) {
1+ jQuery ( function ( $ ) {
22
33// define some basic default data to start with
44var contacts = [
@@ -10,39 +10,36 @@ var contacts = [
1010 { type : "Home" , number : "(555) 999-1212" } ] }
1111] ;
1212
13- // enable using 'contacts' as the name of the template
14- $ . templates . contacts = $ . tmpl ( $ ( "#contacttmpl" ) . html ( ) ) ;
15-
16- $ . extend ( $ . convertFn , {
13+ $ . extend ( $ . convertFn , {
1714 // linking converter that normalizes phone numbers
18- phone : function ( value ) { // turn a string phone number into a normalized one with dashes
15+ phone : function ( value ) { // turn a string phone number into a normalized one with dashes
1916 // and parens
20- value = ( parseInt ( value . replace ( / [ \( \) \- ] / g, "" ) , 10 ) || 0 ) . toString ( ) ;
17+ value = ( parseInt ( value . replace ( / [ \( \) \- ] / g, "" ) , 10 ) || 0 ) . toString ( ) ;
2118 value = "0000000000" + value ;
22- value = value . substr ( value . length - 10 ) ;
23- value = "(" + value . substr ( 0 , 3 ) + ") " + value . substr ( 3 , 3 ) + "-" + value . substr ( 6 ) ;
19+ value = value . substr ( value . length - 10 ) ;
20+ value = "(" + value . substr ( 0 , 3 ) + ") " + value . substr ( 3 , 3 ) + "-" + value . substr ( 6 ) ;
2421 return value ;
2522 } ,
26- fullname : function ( value , source , target ) {
23+ fullname : function ( value , source , target ) {
2724 return source . firstName + " " + source . lastName ;
2825 }
2926} ) ;
3027
3128// show the results of the linking -- object graph is already full of the data
32- $ ( "#save" ) . click ( function ( ) {
33- $ ( "#results" ) . html ( JSON . stringify ( contacts , null , 4 ) ) ;
29+ $ ( "#save" ) . click ( function ( ) {
30+ $ ( "#results" ) . html ( JSON . stringify ( contacts , null , 4 ) ) ;
3431} ) ;
3532
3633// add a new contact when clicking the insert button.
3734// notice that no code here exists that explicitly redraws
3835// the template.
39- $ ( "#insert" ) . click ( function ( ) {
40- contacts . push ( { firstName : "" , lastName : "" , phones : [ ] } ) ;
36+ $ ( "#insert" ) . click ( function ( ) {
37+ contacts . push ( { firstName : "first " , lastName : "last " , phones : [ ] , age : 20 } ) ;
4138 refresh ( ) ;
4239} ) ;
4340
44- $ ( "#sort" ) . click ( function ( ) {
45- contacts . sort ( function ( a , b ) {
41+ $ ( "#sort" ) . click ( function ( ) {
42+ contacts . sort ( function ( a , b ) {
4643 return a . lastName < b . lastName ? - 1 : 1 ;
4744 } ) ;
4845 refresh ( ) ;
@@ -51,55 +48,56 @@ $("#sort").click(function() {
5148// function that clears the current template and renders it with the
5249// current state of the global contacts variable.
5350function refresh ( ) {
54- $ ( ".contacts" ) . empty ( ) . append ( "contacts" , { contacts :contacts } ) ;
51+ $ ( ".contacts" ) . empty ( ) ;
52+ $ ( "#contacttmpl" ) . tmpl ( { contacts :contacts } ) . appendTo ( ".contacts" ) ;
5553 // bind inputs to the data items
56- $ ( "tr.contact" ) . each ( function ( i ) {
54+ $ ( "tr.contact" ) . each ( function ( i ) {
5755 var contact = contacts [ i ] ;
58- $ ( "input.contact" , this ) . link ( contact ) ;
59- $ ( '.agebar' , this ) . link ( contact , {
56+ $ ( "input.contact" , this ) . link ( contact ) ;
57+ $ ( '.agebar' , this ) . link ( contact , {
6058 age : {
61- convertBack : function ( value , source , target ) {
62- $ ( target ) . width ( value + "px" ) ;
59+ convertBack : function ( value , source , target ) {
60+ $ ( target ) . width ( value + "px" ) ;
6361 }
6462 }
6563 } ) ;
66- $ ( contact ) . trigger ( "changeData" , [ "age" , contact . age ] ) ;
64+ $ ( contact ) . trigger ( "changeData" , [ "age" , contact . age ] ) ;
6765
6866 // todo: "update" feature
6967
70- $ ( ".contact-remove" , this ) . click ( function ( ) {
71- contacts . splice ( i , 1 ) ;
68+ $ ( ".contact-remove" , this ) . click ( function ( ) {
69+ contacts . splice ( i , 1 ) ;
7270 refresh ( ) ;
7371 } ) ;
7472 var original_firstName = contact . firstName ,
7573 original_lastName = contact . lastName ;
76- $ ( ".contact-reset" , this ) . click ( function ( ) {
77- $ ( contact )
78- . data ( "firstName" , original_firstName )
79- . data ( "lastName" , original_lastName ) ;
74+ $ ( ".contact-reset" , this ) . click ( function ( ) {
75+ $ ( contact )
76+ . data ( "firstName" , original_firstName )
77+ . data ( "lastName" , original_lastName ) ;
8078 } ) ;
8179
82- $ ( "tr.phone" , this ) . each ( function ( i ) {
80+ $ ( "tr.phone" , this ) . each ( function ( i ) {
8381 var phone = contact . phones [ i ] ;
84- $ ( this ) . link ( phone , {
82+ $ ( this ) . link ( phone , {
8583 type : "type" ,
8684 number : {
8785 name : "number" ,
8886 convert : "phone"
8987 }
9088 } ) ;
91- $ ( ".phone-remove" , this ) . click ( function ( ) {
89+ $ ( ".phone-remove" , this ) . click ( function ( ) {
9290 // note: I'd like to only redraw the phones portion of the
9391 // template, but jquery.tmpl.js does not support nested templates
9492 // very easily. So here I am triggering an arrayChange event on
9593 // the main contacts array to force the entire thing to refresh.
9694 // Note that user input is not lost since the live linking has
9795 // already stored the values in the object graph.
98- contact . phones . splice ( i , 1 ) ;
96+ contact . phones . splice ( i , 1 ) ;
9997 refresh ( ) ;
10098 } ) ;
10199 } ) ;
102- $ ( ".newphone" , this ) . click ( function ( ) {
100+ $ ( ".newphone" , this ) . click ( function ( ) {
103101 contact . phones . push ( { type : "" , number : "" } ) ;
104102 refresh ( ) ;
105103 } ) ;
0 commit comments