Skip to content

Commit 6febd87

Browse files
committed
Handle erorrs occurred on views by hook the router alert() to view error events.
1 parent 8da36bf commit 6febd87

File tree

6 files changed

+21
-37
lines changed

6 files changed

+21
-37
lines changed

public/javascripts/router.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define('router', [
4646

4747
collection.fetch({
4848
success: function(collection, res) {
49-
view = new PostListView({ collection: collection });
49+
view = new PostListView({collection: collection});
5050
that.appView.show(view);
5151
},
5252
error: that.alert
@@ -64,13 +64,14 @@ define('router', [
6464
});
6565
model.fetch({
6666
success: function(model, res) {
67-
view = new PostShowView({ model: model });
67+
view = new PostShowView({model: model});
6868
that.appView.show(view);
6969

70-
view.model.on('success', function() {
70+
view.on('success', function() {
7171
delete view;
72-
that.navigate('#/post/list', { trigger: true });
72+
that.navigate('#/post/list', {trigger: true});
7373
});
74+
view.on('router:alert', that.alert);
7475
},
7576
error: that.alert
7677
});
@@ -83,14 +84,11 @@ define('router', [
8384
});
8485

8586
this.appView.show(view);
86-
view.on('back', function() {
87+
view.on('success', function(id) {
8788
delete view;
88-
that.navigate('#/post/list', { trigger: true });
89-
});
90-
view.model.on('success', function(id) {
91-
delete view;
92-
that.navigate('#/post/' + id, { trigger: true });
89+
that.navigate('#/post/' + id, {trigger: true});
9390
});
91+
view.on('router:alert', that.alert);
9492
},
9593
editPost: function(id) {
9694
var that, view, post;
@@ -103,17 +101,14 @@ define('router', [
103101
});
104102
post.fetch({
105103
success: function(model, res) {
106-
view = new PostEditView({ model: model });
104+
view = new PostEditView({model: model});
107105
that.appView.show(view);
108106

109-
view.on('back', function() {
110-
delete view;
111-
that.navigate('#/post/' + id, { trigger: true });
112-
});
113-
view.model.on('success', function() {
107+
view.on('success', function() {
114108
delete view;
115-
that.navigate('#/post/' + id, { trigger: true });
109+
that.navigate('#/post/' + id, {trigger: true});
116110
});
111+
view.on('router:alert', that.alert);
117112
},
118113
error: that.alert
119114
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<h3 class="title"><%= post.title %></h3>
22
<p class="author"><%= post.author %></p>
33
<p class="body"><%= post.body %></p>
4-
<a href="#/post/<%= post._id %>/edit">Edit</a>&nbsp;|&nbsp;<span class="link delete">Delete</span>
4+
<a href="#/post/<%= post._id %>/edit">Edit</a>
5+
<button id="delete">Delete</button>

public/javascripts/views/post/add.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ define('PostAddView', [
3434
body : body
3535
}, {
3636
success: function(model, res) {
37-
if (res && res.error) {
38-
// TODO
39-
} else {
40-
model.trigger('success', model.id);
41-
}
37+
that.trigger('success', model.id);
4238
},
4339
error: function(model, res) {
44-
// TODO
40+
that.trigger('router:alert');
4541
}
4642
});
4743
},

public/javascripts/views/post/edit.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ define('PostEditView', [
3939
silent : false,
4040
sync : true,
4141
success : function(model, res) {
42-
if (res && res.error) {
43-
// TODO
44-
} else {
45-
model.trigger('success', model.id);
46-
}
42+
that.trigger('success', model.id);
4743
},
4844
error: function(model, res) {
49-
// TODO
45+
that.trigger('router:alert');
5046
}
5147
});
5248
},

public/javascripts/views/post/show.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ define('PostShowView', [
2121
'click #delete': 'delete'
2222
},
2323
delete: function(e) {
24+
var that = this;
2425
e.preventDefault();
2526
this.model.destroy({
2627
sync: true,
2728
success: function(model) {
28-
model.trigger('success');
29+
that.trigger('success');
2930
},
3031
error: function() {
31-
// TODO handle 404 and 500
32+
that.trigger('router:alert');
3233
}
3334
});
3435
}

public/stylesheets/style.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ body {
66
a {
77
color: #00B7FF;
88
}
9-
span.link {
10-
color: #00B7FF;
11-
text-decoration: underline;
12-
cursor: pointer;
13-
}
149

1510
/* header view */
1611
#header-view ul {

0 commit comments

Comments
 (0)