[#57,backend,refactor][s]: create a recline.Backend.Base model which other backends inherit from.

* At the moment this added little other than a cleaner structure but will help with change for #57.
This commit is contained in:
Rufus Pollock
2012-04-01 10:49:22 +01:00
parent 3412962a35
commit 767f9a23d6
5 changed files with 42 additions and 30 deletions

View File

@@ -14,29 +14,41 @@ this.recline.Backend = this.recline.Backend || {};
return model.backend.sync(method, model, options); return model.backend.sync(method, model, options);
} }
// ## wrapInTimeout // ## recline.Backend.Base
// //
// Crude way to catch backend errors // Base class for backends providing a template and convenience functions.
// Many of backends use JSONP and so will not get error messages and this is // You do not have to inherit from this class but even when not it does provide guidance on the functions you must implement.
// a crude way to catch those errors. my.Base = Backbone.Model.extend({
my.wrapInTimeout = function(ourFunction) { sync: function(method, model, options) {
var dfd = $.Deferred(); },
var timeout = 5000; query: function(model, queryObj) {
var timer = setTimeout(function() { },
dfd.reject({
message: 'Request Error: Backend did not respond after ' + (timeout / 1000) + ' seconds' // ## _wrapInTimeout
}); //
}, timeout); // Convenience method providing a crude way to catch backend errors on JSONP calls.
ourFunction.done(function(arguments) { // Many of backends use JSONP and so will not get error messages and this is
clearTimeout(timer); // a crude way to catch those errors.
dfd.resolve(arguments); _wrapInTimeout: function(ourFunction) {
}) var dfd = $.Deferred();
.fail(function(arguments) { var timeout = 5000;
clearTimeout(timer); var timer = setTimeout(function() {
dfd.reject(arguments); dfd.reject({
}) message: 'Request Error: Backend did not respond after ' + (timeout / 1000) + ' seconds'
; });
return dfd.promise(); }, timeout);
} ourFunction.done(function(arguments) {
clearTimeout(timer);
dfd.resolve(arguments);
})
.fail(function(arguments) {
clearTimeout(timer);
dfd.reject(arguments);
})
;
return dfd.promise();
}
});
}(jQuery, this.recline.Backend)); }(jQuery, this.recline.Backend));

View File

@@ -16,7 +16,7 @@ this.recline.Backend = this.recline.Backend || {};
// * format: (optional) csv | xls (defaults to csv if not specified) // * format: (optional) csv | xls (defaults to csv if not specified)
// //
// Note that this is a **read-only** backend. // Note that this is a **read-only** backend.
my.DataProxy = Backbone.Model.extend({ my.DataProxy = my.Base.extend({
defaults: { defaults: {
dataproxy_url: 'http://jsonpdataproxy.appspot.com' dataproxy_url: 'http://jsonpdataproxy.appspot.com'
}, },
@@ -47,7 +47,7 @@ this.recline.Backend = this.recline.Backend || {};
, dataType: 'jsonp' , dataType: 'jsonp'
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
my.wrapInTimeout(jqxhr).done(function(results) { this._wrapInTimeout(jqxhr).done(function(results) {
if (results.error) { if (results.error) {
dfd.reject(results.error); dfd.reject(results.error);
} }

View File

@@ -19,7 +19,7 @@ this.recline.Backend = this.recline.Backend || {};
// localhost:9200 with index twitter and type tweet it would be // localhost:9200 with index twitter and type tweet it would be
// //
// <pre>http://localhost:9200/twitter/tweet</pre> // <pre>http://localhost:9200/twitter/tweet</pre>
my.ElasticSearch = Backbone.Model.extend({ my.ElasticSearch = my.Base.extend({
_getESUrl: function(dataset) { _getESUrl: function(dataset) {
var out = dataset.get('elasticsearch_url'); var out = dataset.get('elasticsearch_url');
if (out) return out; if (out) return out;
@@ -39,7 +39,7 @@ this.recline.Backend = this.recline.Backend || {};
dataType: 'jsonp' dataType: 'jsonp'
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
my.wrapInTimeout(jqxhr).done(function(schema) { this._wrapInTimeout(jqxhr).done(function(schema) {
// only one top level key in ES = the type so we can ignore it // only one top level key in ES = the type so we can ignore it
var key = _.keys(schema)[0]; var key = _.keys(schema)[0];
var fieldData = _.map(schema[key].properties, function(dict, fieldName) { var fieldData = _.map(schema[key].properties, function(dict, fieldName) {

View File

@@ -16,7 +16,7 @@ this.recline.Backend = this.recline.Backend || {};
// 'gdocs' // 'gdocs'
// ); // );
// </pre> // </pre>
my.GDoc = Backbone.Model.extend({ my.GDoc = my.Base.extend({
getUrl: function(dataset) { getUrl: function(dataset) {
var url = dataset.get('url'); var url = dataset.get('url');
if (url.indexOf('feeds/list') != -1) { if (url.indexOf('feeds/list') != -1) {

View File

@@ -69,7 +69,7 @@ this.recline.Backend = this.recline.Backend || {};
// dataset.fetch(); // dataset.fetch();
// etc ... // etc ...
// </pre> // </pre>
my.Memory = Backbone.Model.extend({ my.Memory = my.Base.extend({
initialize: function() { initialize: function() {
this.datasets = {}; this.datasets = {};
}, },