[#128,backend/dataproxy][s]: refactor to new setup.

This commit is contained in:
Rufus Pollock
2012-05-26 16:20:18 +01:00
parent ad7fc1a029
commit 5890007c59
2 changed files with 54 additions and 20 deletions

View File

@@ -1,12 +1,14 @@
this.recline = this.recline || {}; this.recline = this.recline || {};
this.recline.Backend = this.recline.Backend || {}; this.recline.Backend = this.recline.Backend || {};
this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
(function($, my) { (function($, my) {
// ## DataProxy Backend // ## DataProxy Backend
// //
// For connecting to [DataProxy-s](http://github.com/okfn/dataproxy). // For connecting to [DataProxy-s](http://github.com/okfn/dataproxy).
// //
// When initializing the DataProxy backend you can set the following attributes: // When initializing the DataProxy backend you can set the following
// attributes in the options object:
// //
// * dataproxy: {url-to-proxy} (optional). Defaults to http://jsonpdataproxy.appspot.com // * dataproxy: {url-to-proxy} (optional). Defaults to http://jsonpdataproxy.appspot.com
// //
@@ -16,14 +18,14 @@ 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 = my.Base.extend({ my.Backbone = function(options) {
__type__: 'dataproxy', var self = this;
readonly: true, this.__type__ = 'dataproxy';
defaults: { this.readonly = true;
dataproxy_url: 'http://jsonpdataproxy.appspot.com'
}, this.dataproxy_url = options && options.dataproxy_url ? options.dataproxy_url : 'http://jsonpdataproxy.appspot.com';
sync: function(method, model, options) {
var self = this; this.sync = function(method, model, options) {
if (method === "read") { if (method === "read") {
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
// Do nothing as we will get fields in query step (and no metadata to // Do nothing as we will get fields in query step (and no metadata to
@@ -35,22 +37,22 @@ this.recline.Backend = this.recline.Backend || {};
} else { } else {
alert('This backend only supports read operations'); alert('This backend only supports read operations');
} }
}, };
query: function(dataset, queryObj) {
this.query = function(dataset, queryObj) {
var self = this; var self = this;
var base = this.get('dataproxy_url');
var data = { var data = {
url: dataset.get('url'), url: dataset.get('url'),
'max-results': queryObj.size, 'max-results': queryObj.size,
type: dataset.get('format') type: dataset.get('format')
}; };
var jqxhr = $.ajax({ var jqxhr = $.ajax({
url: base, url: this.dataproxy_url,
data: data, data: data,
dataType: 'jsonp' dataType: 'jsonp'
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
this._wrapInTimeout(jqxhr).done(function(results) { _wrapInTimeout(jqxhr).done(function(results) {
if (results.error) { if (results.error) {
dfd.reject(results.error); dfd.reject(results.error);
} }
@@ -65,13 +67,43 @@ this.recline.Backend = this.recline.Backend || {};
}); });
return tmp; return tmp;
}); });
dfd.resolve(self._docsToQueryResult(_out)); dfd.resolve({
total: null,
hits: _.map(_out, function(row) {
return { _source: row };
})
});
}) })
.fail(function(arguments) { .fail(function(arguments) {
dfd.reject(arguments); dfd.reject(arguments);
}); });
return dfd.promise(); return dfd.promise();
} };
}); };
}(jQuery, this.recline.Backend)); // ## _wrapInTimeout
//
// Convenience method providing a crude way to catch backend errors on JSONP calls.
// Many of backends use JSONP and so will not get error messages and this is
// a crude way to catch those errors.
var _wrapInTimeout = function(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();
}
}(jQuery, this.recline.Backend.DataProxy));

View File

@@ -67,8 +67,10 @@ var dataProxyData = {
test('DataProxy Backend', function() { test('DataProxy Backend', function() {
// needed only if not stubbing // needed only if not stubbing
// stop(); // stop();
var backend = new recline.Backend.DataProxy(); var backend = new recline.Backend.DataProxy.Backbone();
ok(backend.readonly, false); console.log(backend.readonly);
ok(backend.readonly);
equal(backend.__type__, 'dataproxy');
var dataset = new recline.Model.Dataset({ var dataset = new recline.Model.Dataset({
url: 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.csv' url: 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.csv'