66let ns = { } ;
77
88// Create the model instance
9- ns . model = ( function ( ) {
9+ ns . model = ( function ( ) {
1010 'use strict' ;
1111
12- let $event_pump = $ ( 'body' ) ;
13-
1412 // Return the API
1513 return {
16- 'read' : function ( ) {
14+ 'read' : function ( ) {
1715 let ajax_options = {
1816 type : 'GET' ,
1917 url : '/api/notes' ,
2018 accepts : 'application/json' ,
2119 dataType : 'json'
2220 } ;
23- $ . ajax ( ajax_options )
24- . done ( function ( data ) {
25- $event_pump . trigger ( 'model_read_success' , [ data ] ) ;
26- } )
27- . fail ( function ( xhr , textStatus , errorThrown ) {
28- $event_pump . trigger ( 'model_error' , [ xhr , textStatus , errorThrown ] ) ;
29- } ) ;
21+ return $ . ajax ( ajax_options ) ;
3022 }
3123 } ;
3224} ( ) ) ;
3325
3426
3527// Create the view instance
36- ns . view = ( function ( ) {
28+ ns . view = ( function ( ) {
3729 'use strict' ;
3830
3931 var $table = $ ( ".blog table" ) ;
4032
4133 // Return the API
4234 return {
43- build_table : function ( data ) {
35+ build_table : function ( data ) {
4436 let source = $ ( '#blog-table-template' ) . html ( ) ,
4537 template = Handlebars . compile ( source ) ,
4638 html ;
@@ -51,58 +43,60 @@ ns.view = (function() {
5143 // Append the rows to the table tbody
5244 $table . append ( html ) ;
5345 } ,
54- error : function ( error_msg ) {
46+ error : function ( error_msg ) {
5547 $ ( '.error' )
5648 . text ( error_msg )
5749 . css ( 'visibility' , 'visible' ) ;
58- setTimeout ( function ( ) {
59- $ ( '.error' ) . css ( 'visibility' , 'hidden' ) ;
60- } , 3000 )
50+ setTimeout ( function ( ) {
51+ $ ( '.error' ) . fadeOut ( ) ;
52+ } , 2000 )
6153 }
6254 } ;
6355} ( ) ) ;
6456
6557
6658// Create the controller instance
67- ns . controller = ( function ( m , v ) {
59+ ns . controller = ( function ( m , v ) {
6860 'use strict' ;
6961
7062 let model = m ,
71- view = v ,
72- $event_pump = $ ( 'body' ) ;
63+ view = v ;
7364
7465 // Get the note data from the model after the controller is done initializing
75- setTimeout ( function ( ) {
76- model . read ( ) ;
66+ setTimeout ( function ( ) {
67+
68+ // Attach event handlers to the promise returned by model.read()
69+ model . read ( )
70+ . done ( function ( data ) {
71+ view . build_table ( data ) ;
72+ } )
73+ . fail ( function ( xhr , textStatus , errorThrown ) {
74+ error_handler ( xhr , textStatus , errorThrown ) ;
75+ } ) ;
7776 } , 100 ) ;
7877
79- // handle application events
80- $ ( 'table' ) . on ( 'dblclick' , 'tbody td.name' , function ( e ) {
81- let $target = $ ( e . target ) . parent ( ) ,
82- person_id = $target . data ( 'person_id' ) ;
83-
84- window . location = `/people/${ person_id } ` ;
78+ // generic error handler
79+ function error_handler ( xhr , textStatus , errorThrown ) {
80+ let error_msg = `${ textStatus } : ${ errorThrown } - ${ xhr . responseJSON . detail } ` ;
8581
86- } ) ;
82+ view . error ( error_msg ) ;
83+ console . log ( error_msg ) ;
84+ }
8785
88- $ ( 'table' ) . on ( 'dblclick' , 'tbody td.content' , function ( e ) {
89- let $target = $ ( e . target ) . parent ( ) ,
90- person_id = $target . data ( 'person_id' ) ,
91- note_id = $target . data ( 'note_id ' ) ;
86+ // handle application events
87+ $ ( 'table' ) . on ( 'dblclick' , 'tbody td.name' , function ( e ) {
88+ let $target = $ ( e . target ) . parent ( ) ,
89+ person_id = $target . data ( 'person_id ' ) ;
9290
93- window . location = `people/${ person_id } /notes/${ note_id } ` ;
94- } ) ;
91+ window . location = `/people/${ person_id } ` ;
9592
96- // Handle the model events
97- $event_pump . on ( 'model_read_success' , function ( e , data ) {
98- view . build_table ( data ) ;
9993 } ) ;
10094
101- $event_pump . on ( 'model_error' , function ( e , xhr , textStatus , errorThrown ) {
102- let error_msg = textStatus + ': ' + errorThrown + ' - ' + xhr . responseJSON . detail ;
103- view . error ( error_msg ) ;
104- console . log ( error_msg ) ;
105- } )
106-
95+ $ ( 'table' ) . on ( 'dblclick' , 'tbody td.content' , function ( e ) {
96+ let $target = $ ( e . target ) . parent ( ) ,
97+ person_id = $target . data ( 'person_id' ) ,
98+ note_id = $target . data ( 'note_id' ) ;
10799
100+ window . location = `people/${ person_id } /notes/${ note_id } ` ;
101+ } ) ;
108102} ( ns . model , ns . view ) ) ;
0 commit comments