[#172,refactor][s]: switch everything to use underscore.deferred rather than jQuery.Deferred - fixes #172.
* In addition reduced pattern of passing in $ to backend modules - instead just use jQuery explicitly (this should make it easier to mock-out jQuery if you waned to
This commit is contained in:
@@ -2,7 +2,7 @@ this.recline = this.recline || {};
|
||||
this.recline.Backend = this.recline.Backend || {};
|
||||
this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
|
||||
|
||||
(function($, my) {
|
||||
(function(my) {
|
||||
// ## CKAN Backend
|
||||
//
|
||||
// This provides connection to the CKAN DataStore (v2)
|
||||
@@ -41,7 +41,7 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
|
||||
dataset.id = out.resource_id;
|
||||
var wrapper = my.DataStore(out.endpoint);
|
||||
}
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var jqxhr = wrapper.search({resource_id: dataset.id, limit: 0});
|
||||
jqxhr.done(function(results) {
|
||||
// map ckan types to our usual types ...
|
||||
@@ -84,7 +84,7 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
|
||||
var wrapper = my.DataStore(out.endpoint);
|
||||
}
|
||||
var actualQuery = my._normalizeQuery(queryObj, dataset);
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var jqxhr = wrapper.search(actualQuery);
|
||||
jqxhr.done(function(results) {
|
||||
var out = {
|
||||
@@ -107,7 +107,7 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
|
||||
};
|
||||
that.search = function(data) {
|
||||
var searchUrl = that.endpoint + '/3/action/datastore_search';
|
||||
var jqxhr = $.ajax({
|
||||
var jqxhr = jQuery.ajax({
|
||||
url: searchUrl,
|
||||
data: data,
|
||||
dataType: 'json'
|
||||
@@ -136,4 +136,4 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
|
||||
'float8': 'float'
|
||||
};
|
||||
|
||||
}(jQuery, this.recline.Backend.Ckan));
|
||||
}(this.recline.Backend.Ckan));
|
||||
|
||||
@@ -197,7 +197,7 @@ my.__type__ = 'couchdb';
|
||||
var db_url = dataset.db_url;
|
||||
var view_url = dataset.view_url;
|
||||
var cdb = new my.CouchDBWrapper(db_url, view_url);
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
|
||||
// if 'doc' attribute is present, return schema of that
|
||||
// else return schema of 'value' attribute which contains
|
||||
@@ -239,7 +239,7 @@ my.__type__ = 'couchdb';
|
||||
//
|
||||
//
|
||||
my.save = function (changes, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var total = changes.creates.length + changes.updates.length + changes.deletes.length;
|
||||
var results = {'done': [], 'fail': [] };
|
||||
|
||||
@@ -280,7 +280,7 @@ my.save = function (changes, dataset) {
|
||||
// @param {Object} recline.Dataset instance
|
||||
// @param {Object} recline.Query instance.
|
||||
my.query = function(queryObj, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var db_url = dataset.db_url;
|
||||
var view_url = dataset.view_url;
|
||||
var query_options = dataset.query_options;
|
||||
@@ -475,7 +475,7 @@ function randomId(length, chars) {
|
||||
}
|
||||
|
||||
_createDocument = function (new_doc, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var db_url = dataset.db_url;
|
||||
var view_url = dataset.view_url;
|
||||
var _id = new_doc['id'];
|
||||
@@ -497,7 +497,7 @@ _createDocument = function (new_doc, dataset) {
|
||||
};
|
||||
|
||||
_updateDocument = function (new_doc, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var db_url = dataset.db_url;
|
||||
var view_url = dataset.view_url;
|
||||
var _id = new_doc['id'];
|
||||
@@ -527,7 +527,7 @@ _updateDocument = function (new_doc, dataset) {
|
||||
};
|
||||
|
||||
_deleteDocument = function (del_doc, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var db_url = dataset.db_url;
|
||||
var view_url = dataset.view_url;
|
||||
var _id = del_doc['id'];
|
||||
@@ -557,4 +557,4 @@ _deleteDocument = function (del_doc, dataset) {
|
||||
return dfd.promise();
|
||||
}
|
||||
};
|
||||
}(jQuery, this.recline.Backend.CouchDB));
|
||||
}(jQuery, this.recline.Backend.CouchDB));
|
||||
|
||||
@@ -3,7 +3,7 @@ this.recline.Backend = this.recline.Backend || {};
|
||||
this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
|
||||
// Note that provision of jQuery is optional (it is **only** needed if you use fetch on a remote file)
|
||||
(function(my, $) {
|
||||
(function(my) {
|
||||
|
||||
// ## fetch
|
||||
//
|
||||
@@ -11,7 +11,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
//
|
||||
// 1. `dataset.file`: `file` is an HTML5 file object. This is opened and parsed with the CSV parser.
|
||||
// 2. `dataset.data`: `data` is a string in CSV format. This is passed directly to the CSV parser
|
||||
// 3. `dataset.url`: a url to an online CSV file that is ajax accessible (note this usually requires either local or on a server that is CORS enabled). The file is then loaded using $.ajax and parsed using the CSV parser (NB: this requires jQuery)
|
||||
// 3. `dataset.url`: a url to an online CSV file that is ajax accessible (note this usually requires either local or on a server that is CORS enabled). The file is then loaded using jQuery.ajax and parsed using the CSV parser (NB: this requires jQuery)
|
||||
//
|
||||
// All options generates similar data and use the memory store outcome, that is they return something like:
|
||||
//
|
||||
@@ -23,7 +23,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
// }
|
||||
// </pre>
|
||||
my.fetch = function(dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
if (dataset.file) {
|
||||
var reader = new FileReader();
|
||||
var encoding = dataset.encoding || 'UTF-8';
|
||||
@@ -48,7 +48,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
useMemoryStore: true
|
||||
});
|
||||
} else if (dataset.url) {
|
||||
$.get(dataset.url).done(function(data) {
|
||||
jQuery.get(dataset.url).done(function(data) {
|
||||
var rows = my.parseCSV(data, dataset);
|
||||
dfd.resolve({
|
||||
records: rows,
|
||||
@@ -285,4 +285,4 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
}
|
||||
|
||||
|
||||
}(this.recline.Backend.CSV, jQuery));
|
||||
}(this.recline.Backend.CSV));
|
||||
|
||||
@@ -2,7 +2,7 @@ this.recline = this.recline || {};
|
||||
this.recline.Backend = this.recline.Backend || {};
|
||||
this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
|
||||
|
||||
(function($, my) {
|
||||
(function(my) {
|
||||
my.__type__ = 'dataproxy';
|
||||
// URL for the dataproxy
|
||||
my.dataproxy_url = 'http://jsonpdataproxy.appspot.com';
|
||||
@@ -21,12 +21,12 @@ this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
|
||||
'max-results': dataset.size || dataset.rows || 1000,
|
||||
type: dataset.format || ''
|
||||
};
|
||||
var jqxhr = $.ajax({
|
||||
var jqxhr = jQuery.ajax({
|
||||
url: my.dataproxy_url,
|
||||
data: data,
|
||||
dataType: 'jsonp'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
_wrapInTimeout(jqxhr).done(function(results) {
|
||||
if (results.error) {
|
||||
dfd.reject(results.error);
|
||||
@@ -50,7 +50,7 @@ this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
|
||||
// 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 dfd = new _.Deferred();
|
||||
var timer = setTimeout(function() {
|
||||
dfd.reject({
|
||||
message: 'Request Error: Backend did not respond after ' + (my.timeout / 1000) + ' seconds'
|
||||
@@ -68,4 +68,4 @@ this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
|
||||
return dfd.promise();
|
||||
}
|
||||
|
||||
}(jQuery, this.recline.Backend.DataProxy));
|
||||
}(this.recline.Backend.DataProxy));
|
||||
|
||||
@@ -179,7 +179,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
|
||||
// ### fetch
|
||||
my.fetch = function(dataset) {
|
||||
var es = new my.Wrapper(dataset.url, my.esOptions);
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
es.mapping().done(function(schema) {
|
||||
|
||||
if (!schema){
|
||||
@@ -207,7 +207,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
|
||||
my.save = function(changes, dataset) {
|
||||
var es = new my.Wrapper(dataset.url, my.esOptions);
|
||||
if (changes.creates.length + changes.updates.length + changes.deletes.length > 1) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
msg = 'Saving more than one item at a time not yet supported';
|
||||
alert(msg);
|
||||
dfd.reject(msg);
|
||||
@@ -225,7 +225,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
|
||||
|
||||
// ### query
|
||||
my.query = function(queryObj, dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var es = new my.Wrapper(dataset.url, my.esOptions);
|
||||
var jqxhr = es.query(queryObj);
|
||||
jqxhr.done(function(results) {
|
||||
|
||||
@@ -2,7 +2,7 @@ this.recline = this.recline || {};
|
||||
this.recline.Backend = this.recline.Backend || {};
|
||||
this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
||||
|
||||
(function($, my) {
|
||||
(function(my) {
|
||||
my.__type__ = 'gdocs';
|
||||
|
||||
// ## Google spreadsheet backend
|
||||
@@ -29,15 +29,15 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
||||
// * fields: array of Field objects
|
||||
// * records: array of objects for each row
|
||||
my.fetch = function(dataset) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var urls = my.getGDocsAPIUrls(dataset.url);
|
||||
|
||||
// TODO cover it with tests
|
||||
// get the spreadsheet title
|
||||
(function () {
|
||||
var titleDfd = $.Deferred();
|
||||
var titleDfd = new _.Deferred();
|
||||
|
||||
$.getJSON(urls.spreadsheet, function (d) {
|
||||
jQuery.getJSON(urls.spreadsheet, function (d) {
|
||||
titleDfd.resolve({
|
||||
spreadsheetTitle: d.feed.title.$t
|
||||
});
|
||||
@@ -47,7 +47,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
||||
}()).then(function (response) {
|
||||
|
||||
// get the actual worksheet data
|
||||
$.getJSON(urls.worksheet, function(d) {
|
||||
jQuery.getJSON(urls.worksheet, function(d) {
|
||||
var result = my.parseData(d);
|
||||
var fields = _.map(result.fields, function(fieldId) {
|
||||
return {id: fieldId};
|
||||
@@ -161,4 +161,4 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
||||
|
||||
return urls;
|
||||
};
|
||||
}(jQuery, this.recline.Backend.GDocs));
|
||||
}(this.recline.Backend.GDocs));
|
||||
|
||||
@@ -2,7 +2,7 @@ this.recline = this.recline || {};
|
||||
this.recline.Backend = this.recline.Backend || {};
|
||||
this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
|
||||
(function($, my) {
|
||||
(function(my) {
|
||||
my.__type__ = 'memory';
|
||||
|
||||
// ## Data Wrapper
|
||||
@@ -48,7 +48,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
|
||||
this.save = function(changes, dataset) {
|
||||
var self = this;
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
// TODO _.each(changes.creates) { ... }
|
||||
_.each(changes.updates, function(record) {
|
||||
self.update(record);
|
||||
@@ -61,7 +61,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
},
|
||||
|
||||
this.query = function(queryObj) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
var numRows = queryObj.size || this.records.length;
|
||||
var start = queryObj.from || 0;
|
||||
var results = this.records;
|
||||
@@ -229,7 +229,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
};
|
||||
|
||||
this.transform = function(editFunc) {
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
// TODO: should we clone before mapping? Do not see the point atm.
|
||||
self.records = _.map(self.records, editFunc);
|
||||
// now deal with deletes (i.e. nulls)
|
||||
@@ -241,4 +241,4 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
};
|
||||
};
|
||||
|
||||
}(jQuery, this.recline.Backend.Memory));
|
||||
}(this.recline.Backend.Memory));
|
||||
|
||||
@@ -18,7 +18,7 @@ this.recline.Backend.Solr = this.recline.Backend.Solr || {};
|
||||
dataType: 'jsonp',
|
||||
jsonp: 'json.wrf'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
jqxhr.done(function(results) {
|
||||
// if we get 0 results we cannot get fields
|
||||
var fields = []
|
||||
@@ -51,7 +51,7 @@ this.recline.Backend.Solr = this.recline.Backend.Solr || {};
|
||||
dataType: 'jsonp',
|
||||
jsonp: 'json.wrf'
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
jqxhr.done(function(results) {
|
||||
var out = {
|
||||
total: results.response.numFound,
|
||||
|
||||
10
src/model.js
10
src/model.js
@@ -2,7 +2,7 @@
|
||||
this.recline = this.recline || {};
|
||||
this.recline.Model = this.recline.Model || {};
|
||||
|
||||
(function($, my) {
|
||||
(function(my) {
|
||||
|
||||
// ## <a id="dataset">Dataset</a>
|
||||
my.Dataset = Backbone.Model.extend({
|
||||
@@ -47,7 +47,7 @@ my.Dataset = Backbone.Model.extend({
|
||||
// Retrieve dataset and (some) records from the backend.
|
||||
fetch: function() {
|
||||
var self = this;
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
|
||||
if (this.backend !== recline.Backend.Memory) {
|
||||
this.backend.fetch(this.toJSON())
|
||||
@@ -181,7 +181,7 @@ my.Dataset = Backbone.Model.extend({
|
||||
// also returned.
|
||||
query: function(queryObj) {
|
||||
var self = this;
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
this.trigger('query:start');
|
||||
|
||||
if (queryObj) {
|
||||
@@ -245,7 +245,7 @@ my.Dataset = Backbone.Model.extend({
|
||||
this.fields.each(function(field) {
|
||||
query.addFacet(field.id);
|
||||
});
|
||||
var dfd = $.Deferred();
|
||||
var dfd = new _.Deferred();
|
||||
this._store.query(query.toJSON(), this.toJSON()).done(function(queryResult) {
|
||||
if (queryResult.facets) {
|
||||
_.each(queryResult.facets, function(facetResult, facetId) {
|
||||
@@ -585,5 +585,5 @@ Backbone.sync = function(method, model, options) {
|
||||
return model.backend.sync(method, model, options);
|
||||
};
|
||||
|
||||
}(jQuery, this.recline.Model));
|
||||
}(this.recline.Model));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user