1+ $ ( ( ) => {
2+ const templates = { } ;
3+ const phonebook = { contacts : [ ] } ;
4+ const divList = $ ( "#list .content" ) ;
5+ const divDetails = $ ( "#details .content" ) ;
6+ loadData ( ) ;
7+
8+ async function loadData ( ) {
9+ phonebook . contacts = ( await $ . get ( "data.json" ) ) . map ( c => {
10+ c . active = false ;
11+ return c ;
12+ } ) ;
13+ }
14+
15+ loadTemplates ( ) ;
16+
17+ async function loadTemplates ( ) {
18+
19+ const [ contactSource , listSource , detailsSource ] = await Promise . all ( [
20+ $ . get ( "./templates/contacts.html" ) ,
21+ $ . get ( "./templates/contactList.html" ) ,
22+ $ . get ( "./templates/details.html" )
23+ ] ) ;
24+
25+ Handlebars . registerPartial ( "contact" , contactSource ) ;
26+ templates . list = Handlebars . compile ( listSource ) ;
27+ templates . details = Handlebars . compile ( detailsSource ) ;
28+
29+ renderList ( ) ;
30+ }
31+
32+ function renderList ( ) {
33+ divList . html ( templates . list ( phonebook ) ) ;
34+ attachEventListener ( ) ;
35+ }
36+
37+ function renderDetails ( id ) {
38+ divDetails . html ( templates . details ( phonebook . contacts [ id ] ) )
39+ console . log ( phonebook . contacts [ id ] )
40+ }
41+
42+ function attachEventListener ( ) {
43+ $ ( ".contact" ) . on ( "click" , ( e ) => {
44+ let index = $ ( e . target ) . closest ( ".contact" ) . attr ( "data-id" ) ;
45+ phonebook . contacts . filter ( c => c . active ) . forEach ( c => c . active = false ) ;
46+ phonebook . contacts [ index ] . active = true ;
47+ renderDetails ( index ) ;
48+ renderList ( ) ;
49+ } )
50+ }
51+ } ) ;
0 commit comments