[view,backend/dataproxy][s]: handle errors from dataproxy better and notify errors better in view.

This commit is contained in:
Rufus Pollock
2012-02-29 22:01:37 +00:00
parent e6ce2ca533
commit fc979023e6
3 changed files with 51 additions and 31 deletions

View File

@@ -1092,17 +1092,30 @@ my.DataExplorer = Backbone.View.extend({
this.router = new Backbone.Router(); this.router = new Backbone.Router();
this.setupRouting(); this.setupRouting();
this.model.bind('query:start', function(eventName) { this.model.bind('query:start', function() {
my.notify('Loading data', {loader: true}); my.notify('Loading data', {loader: true});
}); });
this.model.bind('query:done', function(eventName) { this.model.bind('query:done', function() {
my.clearNotifications(); my.clearNotifications();
self.el.find('.doc-count').text(self.model.docCount || 'Unknown'); self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
my.notify('Data loaded', {category: 'success'}); my.notify('Data loaded', {category: 'success'});
}); });
this.model.bind('query:fail', function(eventName, error) { this.model.bind('query:fail', function(error) {
my.clearNotifications(); my.clearNotifications();
my.notify(error.message, {category: 'error', persist: true}); var msg = '';
if (typeof(error) == 'string') {
msg = error;
} else if (typeof(error) == 'object') {
if (error.title) {
msg = error.title + ': ';
}
if (error.message) {
msg += error.message;
}
} else {
msg = 'There was an error querying the backend';
}
my.notify(msg, {category: 'error', persist: true});
}); });
// retrieve basic data like fields etc // retrieve basic data like fields etc
@@ -1594,29 +1607,10 @@ this.recline.Backend = this.recline.Backend || {};
var self = this; var self = this;
if (method === "read") { if (method === "read") {
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
var base = self.get('dataproxy_url'); // Do nothing as we will get fields in query step (and no metadata to
// TODO: should we cache for extra efficiency // retrieve)
var data = {
url: model.get('url')
, 'max-results': 1
, type: model.get('format') || 'csv'
};
var jqxhr = $.ajax({
url: base
, data: data
, dataType: 'jsonp'
});
var dfd = $.Deferred(); var dfd = $.Deferred();
my.wrapInTimeout(jqxhr).done(function(results) { dfd.resolve(model);
model.fields.reset(_.map(results.fields, function(fieldId) {
return {id: fieldId};
})
);
dfd.resolve(model, jqxhr);
})
.fail(function(arguments) {
dfd.reject(arguments);
});
return dfd.promise(); return dfd.promise();
} }
} else { } else {
@@ -1636,7 +1630,14 @@ this.recline.Backend = this.recline.Backend || {};
, dataType: 'jsonp' , dataType: 'jsonp'
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
jqxhr.done(function(results) { my.wrapInTimeout(jqxhr).done(function(results) {
if (results.error) {
dfd.reject(results.error);
}
dataset.fields.reset(_.map(results.fields, function(fieldId) {
return {id: fieldId};
})
);
var _out = _.map(results.data, function(doc) { var _out = _.map(results.data, function(doc) {
var tmp = {}; var tmp = {};
_.each(results.fields, function(key, idx) { _.each(results.fields, function(key, idx) {
@@ -1645,6 +1646,9 @@ this.recline.Backend = this.recline.Backend || {};
return tmp; return tmp;
}); });
dfd.resolve(_out); dfd.resolve(_out);
})
.fail(function(arguments) {
dfd.reject(arguments);
}); });
return dfd.promise(); return dfd.promise();
} }

View File

@@ -48,6 +48,9 @@ this.recline.Backend = this.recline.Backend || {};
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
my.wrapInTimeout(jqxhr).done(function(results) { my.wrapInTimeout(jqxhr).done(function(results) {
if (results.error) {
dfd.reject(results.error);
}
dataset.fields.reset(_.map(results.fields, function(fieldId) { dataset.fields.reset(_.map(results.fields, function(fieldId) {
return {id: fieldId}; return {id: fieldId};
}) })

View File

@@ -104,17 +104,30 @@ my.DataExplorer = Backbone.View.extend({
this.router = new Backbone.Router(); this.router = new Backbone.Router();
this.setupRouting(); this.setupRouting();
this.model.bind('query:start', function(eventName) { this.model.bind('query:start', function() {
my.notify('Loading data', {loader: true}); my.notify('Loading data', {loader: true});
}); });
this.model.bind('query:done', function(eventName) { this.model.bind('query:done', function() {
my.clearNotifications(); my.clearNotifications();
self.el.find('.doc-count').text(self.model.docCount || 'Unknown'); self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
my.notify('Data loaded', {category: 'success'}); my.notify('Data loaded', {category: 'success'});
}); });
this.model.bind('query:fail', function(eventName, error) { this.model.bind('query:fail', function(error) {
my.clearNotifications(); my.clearNotifications();
my.notify(error.message, {category: 'error', persist: true}); var msg = '';
if (typeof(error) == 'string') {
msg = error;
} else if (typeof(error) == 'object') {
if (error.title) {
msg = error.title + ': ';
}
if (error.message) {
msg += error.message;
}
} else {
msg = 'There was an error querying the backend';
}
my.notify(msg, {category: 'error', persist: true});
}); });
// retrieve basic data like fields etc // retrieve basic data like fields etc