Skip to content

Commit

Permalink
[datopian#128,backend/dataproxy][s]: refactor to new setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rufuspollock committed May 26, 2012
1 parent ad7fc1a commit 5890007
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
68 changes: 50 additions & 18 deletions src/backend/dataproxy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
this.recline = this.recline || {};
this.recline.Backend = this.recline.Backend || {};
this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};

(function($, my) {
// ## DataProxy Backend
//
// 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
//
Expand All @@ -16,14 +18,14 @@ this.recline.Backend = this.recline.Backend || {};
// * format: (optional) csv | xls (defaults to csv if not specified)
//
// Note that this is a **read-only** backend.
my.DataProxy = my.Base.extend({
__type__: 'dataproxy',
readonly: true,
defaults: {
dataproxy_url: 'http://jsonpdataproxy.appspot.com'
},
sync: function(method, model, options) {
var self = this;
my.Backbone = function(options) {
var self = this;
this.__type__ = 'dataproxy';
this.readonly = true;

this.dataproxy_url = options && options.dataproxy_url ? options.dataproxy_url : 'http://jsonpdataproxy.appspot.com';

this.sync = function(method, model, options) {
if (method === "read") {
if (model.__type__ == 'Dataset') {
// Do nothing as we will get fields in query step (and no metadata to
Expand All @@ -35,22 +37,22 @@ this.recline.Backend = this.recline.Backend || {};
} else {
alert('This backend only supports read operations');
}
},
query: function(dataset, queryObj) {
};

this.query = function(dataset, queryObj) {
var self = this;
var base = this.get('dataproxy_url');
var data = {
url: dataset.get('url'),
'max-results': queryObj.size,
type: dataset.get('format')
};
var jqxhr = $.ajax({
url: base,
url: this.dataproxy_url,
data: data,
dataType: 'jsonp'
});
var dfd = $.Deferred();
this._wrapInTimeout(jqxhr).done(function(results) {
_wrapInTimeout(jqxhr).done(function(results) {
if (results.error) {
dfd.reject(results.error);
}
Expand All @@ -65,13 +67,43 @@ this.recline.Backend = this.recline.Backend || {};
});
return tmp;
});
dfd.resolve(self._docsToQueryResult(_out));
dfd.resolve({
total: null,
hits: _.map(_out, function(row) {
return { _source: row };
})
});
})
.fail(function(arguments) {
dfd.reject(arguments);
});
return dfd.promise();
}
});
};
};

// ## _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));
}(jQuery, this.recline.Backend.DataProxy));
6 changes: 4 additions & 2 deletions test/backend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ var dataProxyData = {
test('DataProxy Backend', function() {
// needed only if not stubbing
// stop();
var backend = new recline.Backend.DataProxy();
ok(backend.readonly, false);
var backend = new recline.Backend.DataProxy.Backbone();
console.log(backend.readonly);
ok(backend.readonly);
equal(backend.__type__, 'dataproxy');

var dataset = new recline.Model.Dataset({
url: 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.csv'
Expand Down

0 comments on commit 5890007

Please sign in to comment.