Skip to content

Commit 4eaa012

Browse files
committed
Changed the JavaScript to use promises rather than triggered events. Cleaner, more modern approach to handling my MVC system.
1 parent 3ae7cce commit 4eaa012

File tree

10 files changed

+191
-197
lines changed

10 files changed

+191
-197
lines changed

flask-connexion-rest-part-3/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class Note(db.Model):
3232

3333

3434
class PersonSchema(ma.ModelSchema):
35+
def __init__(self, **kwargs):
36+
super().__init__(strict=True, **kwargs)
37+
3538
class Meta:
3639
model = Person
3740
sqla_session = db.session
@@ -44,13 +47,19 @@ class PersonNoteSchema(ma.ModelSchema):
4447
This class exists to get around a recursion issue
4548
"""
4649

50+
def __init__(self, **kwargs):
51+
super().__init__(strict=True, **kwargs)
52+
4753
note_id = fields.Int()
4854
person_id = fields.Int()
4955
content = fields.Str()
5056
timestamp = fields.Str()
5157

5258

5359
class NoteSchema(ma.ModelSchema):
60+
def __init__(self, **kwargs):
61+
super().__init__(strict=True, **kwargs)
62+
5463
class Meta:
5564
model = Note
5665
sqla_session = db.session
@@ -63,6 +72,9 @@ class NotePersonSchema(ma.ModelSchema):
6372
This class exists to get around a recursion issue
6473
"""
6574

75+
def __init__(self, **kwargs):
76+
super().__init__(strict=True, **kwargs)
77+
6678
person_id = fields.Int()
6779
lname = fields.Str()
6880
fname = fields.Str()

flask-connexion-rest-part-3/requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ itsdangerous==1.1.0
1212
Jinja2==2.10
1313
jsonschema==2.6.0
1414
MarkupSafe==1.1.0
15-
marshmallow==2.17.0
15+
marshmallow==2.18.0
1616
marshmallow-sqlalchemy==0.15.0
1717
openapi-spec-validator==0.2.4
18-
PyYAML==3.13
18+
PyYAML==4.2b4
1919
requests==2.21.0
2020
six==1.12.0
21-
SQLAlchemy==1.2.16
22-
swagger-ui-bundle==0.0.2
21+
SQLAlchemy==1.2.17
22+
swagger-ui-bundle==0.0.3
2323
urllib3==1.24.1
2424
Werkzeug==0.14.1

flask-connexion-rest-part-3/static/css/home.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ tbody tr:not(:last-child){
6262

6363
tbody tr:hover {
6464
background-color: powderblue;
65-
}
65+
}

flask-connexion-rest-part-3/static/css/notes.css

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,3 @@ tbody tr:hover {
108108
.timestamp {
109109
width: 30%;
110110
}
111-
112-
.error {
113-
width: 50%;
114-
margin-left: auto;
115-
margin-right: auto;
116-
padding: 5px;
117-
border: 1px solid lightgrey;
118-
border-radius: 3px;
119-
background-color: #fbb;
120-
visibility: hidden;
121-
}

flask-connexion-rest-part-3/static/css/parent.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ body, .ui-btn {
4141
.container {
4242
padding: 10px;
4343
}
44+
45+
.error {
46+
width: 50%;
47+
margin-left: auto;
48+
margin-right: auto;
49+
padding: 5px;
50+
border: 1px solid lightgrey;
51+
border-radius: 3px;
52+
background-color: #fbb;
53+
visibility: hidden;
54+
}

flask-connexion-rest-part-3/static/js/home.js

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,33 @@
66
let 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

Comments
 (0)