[#35,all][m]: crude error catching for backends based on timeouts (best we can do with JSONP) - fixes #35.
* Show errors in UI * required some extensive refactoring to use done/fail on promise rather than then
This commit is contained in:
@@ -11,10 +11,38 @@ this.recline.Model = this.recline.Model || {};
|
||||
(function($, my) {
|
||||
my.backends = {};
|
||||
|
||||
// ## Backbone.sync
|
||||
//
|
||||
// Override Backbone.sync to hand off to sync function in relevant backend
|
||||
Backbone.sync = function(method, model, options) {
|
||||
return my.backends[model.backendConfig.type].sync(method, model, options);
|
||||
}
|
||||
|
||||
// ## wrapInTimeout
|
||||
//
|
||||
// Crude way to catch backend errors
|
||||
// Many of backends use JSONP and so will not get error messages and this is
|
||||
// a crude way to catch those errors.
|
||||
function wrapInTimeout(ourFunction) {
|
||||
var dfd = $.Deferred();
|
||||
var timeout = 5000;
|
||||
var timer = setTimeout(function() {
|
||||
dfd.reject({
|
||||
message: 'Request Error: Backend did not respond after ' + (timeout / 1000) + ' seconds'
|
||||
});
|
||||
}, timeout);
|
||||
ourFunction.done(function(arguments) {
|
||||
clearTimeout(timer);
|
||||
dfd.resolve(arguments);
|
||||
})
|
||||
.fail(function(arguments) {
|
||||
clearTimeout(timer);
|
||||
dfd.reject(arguments);
|
||||
})
|
||||
;
|
||||
return dfd.promise();
|
||||
}
|
||||
|
||||
// ## BackendMemory - uses in-memory data
|
||||
//
|
||||
// To use you should:
|
||||
@@ -122,7 +150,7 @@ this.recline.Model = this.recline.Model || {};
|
||||
jsonp: '_callback'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
jqxhr.then(function(schema) {
|
||||
wrapInTimeout(jqxhr).done(function(schema) {
|
||||
headers = _.map(schema.data, function(item) {
|
||||
return item.name;
|
||||
});
|
||||
@@ -131,6 +159,9 @@ this.recline.Model = this.recline.Model || {};
|
||||
});
|
||||
dataset.docCount = schema.count;
|
||||
dfd.resolve(dataset, jqxhr);
|
||||
})
|
||||
.fail(function(arguments) {
|
||||
dfd.reject(arguments);
|
||||
});
|
||||
return dfd.promise();
|
||||
}
|
||||
@@ -150,7 +181,7 @@ this.recline.Model = this.recline.Model || {};
|
||||
cache: true
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
jqxhr.then(function(results) {
|
||||
jqxhr.done(function(results) {
|
||||
dfd.resolve(results.data);
|
||||
});
|
||||
return dfd.promise();
|
||||
@@ -196,11 +227,14 @@ this.recline.Model = this.recline.Model || {};
|
||||
, dataType: 'jsonp'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
jqxhr.then(function(results) {
|
||||
wrapInTimeout(jqxhr).done(function(results) {
|
||||
dataset.set({
|
||||
headers: results.fields
|
||||
});
|
||||
dfd.resolve(dataset, jqxhr);
|
||||
})
|
||||
.fail(function(arguments) {
|
||||
dfd.reject(arguments);
|
||||
});
|
||||
return dfd.promise();
|
||||
}
|
||||
@@ -221,7 +255,7 @@ this.recline.Model = this.recline.Model || {};
|
||||
, dataType: 'jsonp'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
jqxhr.then(function(results) {
|
||||
jqxhr.done(function(results) {
|
||||
var _out = _.map(results.data, function(row) {
|
||||
var tmp = {};
|
||||
_.each(results.fields, function(key, idx) {
|
||||
|
||||
10
src/model.js
10
src/model.js
@@ -35,13 +35,12 @@ this.recline.Model = this.recline.Model || {};
|
||||
// this does not fit very well with Backbone setup. Backbone really expects you to know the ids of objects your are fetching (which you do in classic RESTful ajax-y world). But this paradigm does not fill well with data set up we have here.
|
||||
// This also illustrates the limitations of separating the Dataset and the Backend
|
||||
query: function(queryObj) {
|
||||
this.queryState = queryObj || this.defaultQuery;
|
||||
this.queryState = _.extend({size: 100, offset: 0}, this.queryState);
|
||||
|
||||
var self = this;
|
||||
var backend = my.backends[this.backendConfig.type];
|
||||
this.queryState = queryObj || this.defaultQuery;
|
||||
this.queryState = _.extend({size: 100, offset: 0}, this.queryState);
|
||||
var dfd = $.Deferred();
|
||||
backend.query(this, this.queryState).then(function(rows) {
|
||||
backend.query(this, this.queryState).done(function(rows) {
|
||||
var docs = _.map(rows, function(row) {
|
||||
var _doc = new my.Document(row);
|
||||
_doc.backendConfig = self.backendConfig;
|
||||
@@ -50,6 +49,9 @@ this.recline.Model = this.recline.Model || {};
|
||||
});
|
||||
self.currentDocuments.reset(docs);
|
||||
dfd.resolve(self.currentDocuments);
|
||||
})
|
||||
.fail(function(arguments) {
|
||||
dfd.reject(arguments);
|
||||
});
|
||||
return dfd.promise();
|
||||
},
|
||||
|
||||
16
src/view.js
16
src/view.js
@@ -142,10 +142,14 @@ my.DataExplorer = Backbone.View.extend({
|
||||
|
||||
// retrieve basic data like headers etc
|
||||
// note this.model and dataset returned are the same
|
||||
this.model.fetch().then(function(dataset) {
|
||||
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
|
||||
self.query();
|
||||
});
|
||||
this.model.fetch()
|
||||
.done(function(dataset) {
|
||||
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
|
||||
self.query();
|
||||
})
|
||||
.fail(function(error) {
|
||||
my.notify(error.message, {category: 'error', persist: true});
|
||||
});
|
||||
},
|
||||
|
||||
query: function() {
|
||||
@@ -158,6 +162,10 @@ my.DataExplorer = Backbone.View.extend({
|
||||
.done(function() {
|
||||
my.clearNotifications();
|
||||
my.notify('Data loaded', {category: 'success'});
|
||||
})
|
||||
.fail(function(error) {
|
||||
my.clearNotifications();
|
||||
my.notify(error.message, {category: 'error', persist: true});
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user