Skip to content

Commit

Permalink
Add toError method to Backbone.Controller, Backbone.View to easily co…
Browse files Browse the repository at this point in the history
…nvert an xhr error into a string.
  • Loading branch information
Young Hahn committed Jun 9, 2011
1 parent 111656e commit 05aa81f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
16 changes: 10 additions & 6 deletions controllers/Router.bones
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Backbone.Controller.prototype.toError = function(xhr) {
try {
var err = JSON.parse(xhr.responseText);
return err.message || (err.error && err.error.message) || '';
} catch(err) {
return '';
}
};

controller = Backbone.Controller.extend({
initialize: function(options) {
this.Collection = models.Tilesets;
Expand Down Expand Up @@ -36,12 +45,7 @@ controller = Backbone.Controller.extend({
},
error: function(model, xhr) {
var options = _(this.req.query).clone();
options.error = 'Connection problem.';
try {
var resp = JSON.parse(xhr.responseText);
options.error = resp.message || (resp.error && resp.error.message);
} catch(err) {}

options.error = this.toError(xhr) || 'Connection problem.';
options.view = new views.Error(options);
var view = new views.App(options);
this.res && this.res.send(view.el);
Expand Down
60 changes: 32 additions & 28 deletions views/App.bones
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
// Add `route()` method for handling normally linked paths into hash paths.
// Because IE7 provides an absolute URL for `attr('href')`, regex out the
// internal path and use it as the fragment.
Backbone.View = Backbone.View.extend({
events: {
'click a.route': 'route'
},
href: function(el) {
var href = $(el).get(0).getAttribute('href', 2);
if ($.browser.msie && $.browser.version < 8) {
return /^([a-z]+:\/\/.+?)?(\/.*?)$/.exec(href)[2];
} else {
return href;
}
},
route: function(ev) {
var fragment = _(ev).isString() ? ev : this.href(ev.currentTarget);
if (fragment.charAt(0) === '/') {
// Remove the basepath from the fragment, but leave a /.
fragment = fragment.substr(req.query.basepath.length - 1);
var matched = _.any(Backbone.history.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
if (matched) {
Backbone.history.saveLocation(fragment);
return false;
Backbone.View.prototype.events = { 'click a.route': 'route' };
Backbone.View.prototype.href = function(el) {
var href = $(el).get(0).getAttribute('href', 2);
if ($.browser.msie && $.browser.version < 8) {
return /^([a-z]+:\/\/.+?)?(\/.*?)$/.exec(href)[2];
} else {
return href;
}
};
Backbone.View.prototype.route = function(ev) {
var fragment = _(ev).isString() ? ev : this.href(ev.currentTarget);
if (fragment.charAt(0) === '/') {
// Remove the basepath from the fragment, but leave a /.
fragment = fragment.substr(req.query.basepath.length - 1);
var matched = _.any(Backbone.history.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
if (matched) {
Backbone.history.saveLocation(fragment);
return false;
}
return true;
}
});
return true;
};
Backbone.View.prototype.toError = function(xhr) {
try {
var err = JSON.parse(xhr.responseText);
return err.message || (err.error && err.error.message) || '';
} catch(err) {
return '';
}
};

// App
// ---
Expand Down

0 comments on commit 05aa81f

Please sign in to comment.