[#78,jshint][s]: cleaning up backends as per jshint instructions.

This commit is contained in:
Rufus Pollock 2012-04-10 12:19:39 +01:00
parent 6c22413784
commit 3c9a18ba88
6 changed files with 29 additions and 32 deletions

View File

@ -12,7 +12,7 @@ this.recline.Backend = this.recline.Backend || {};
// Override Backbone.sync to hand off to sync function in relevant backend
Backbone.sync = function(method, model, options) {
return model.backend.sync(method, model, options);
}
};
// ## recline.Backend.Base
//

View File

@ -38,14 +38,14 @@ this.recline.Backend = this.recline.Backend || {};
var self = this;
var base = this.get('dataproxy_url');
var data = {
url: dataset.get('url')
, 'max-results': queryObj.size
, type: dataset.get('format')
url: dataset.get('url'),
'max-results': queryObj.size,
type: dataset.get('format')
};
var jqxhr = $.ajax({
url: base
, data: data
, dataType: 'jsonp'
url: base,
data: data,
dataType: 'jsonp'
});
var dfd = $.Deferred();
this._wrapInTimeout(jqxhr).done(function(results) {

View File

@ -59,37 +59,33 @@ this.recline.Backend = this.recline.Backend || {};
}
},
_normalizeQuery: function(queryObj) {
if (queryObj.toJSON) {
var out = queryObj.toJSON();
} else {
var out = _.extend({}, queryObj);
}
if (out.q != undefined && out.q.trim() === '') {
var out = queryObj.toJSON ? queryObj.toJSON() : _.extend({}, queryObj);
if (out.q !== undefined && out.q.trim() === '') {
delete out.q;
}
if (!out.q) {
out.query = {
match_all: {}
}
};
} else {
out.query = {
query_string: {
query: out.q
}
}
};
delete out.q;
}
// now do filters (note the *plural*)
if (out.filters && out.filters.length) {
if (!out.filter) {
out.filter = {}
out.filter = {};
}
if (!out.filter.and) {
out.filter.and = [];
}
out.filter.and = out.filter.and.concat(out.filters);
}
if (out.filters != undefined) {
if (out.filters !== undefined) {
delete out.filters;
}
return out;
@ -107,10 +103,10 @@ this.recline.Backend = this.recline.Backend || {};
// TODO: fail case
jqxhr.done(function(results) {
_.each(results.hits.hits, function(hit) {
if (!'id' in hit._source && hit._id) {
if (!('id' in hit._source) && hit._id) {
hit._source.id = hit._id;
}
})
});
if (results.facets) {
results.hits.facets = results.facets;
}

View File

@ -23,12 +23,12 @@ this.recline.Backend = this.recline.Backend || {};
return url;
} else {
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/;
var matches = url.match(regex);
if (matches) {
var key = matches[1];
var worksheet = 1;
var out = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values?alt=json'
var out = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values?alt=json';
return out;
} else {
alert('Failed to extract gdocs key from ' + url);
@ -52,8 +52,9 @@ this.recline.Backend = this.recline.Backend || {};
// cache data onto dataset (we have loaded whole gdoc it seems!)
model._dataCache = result.data;
dfd.resolve(model);
})
return dfd.promise(); }
});
return dfd.promise();
}
},
query: function(dataset, queryObj) {
@ -64,7 +65,9 @@ this.recline.Backend = this.recline.Backend || {};
// TODO: factor this out as a common method with other backends
var objs = _.map(dataset._dataCache, function (d) {
var obj = {};
_.each(_.zip(fields, d), function (x) { obj[x[0]] = x[1]; })
_.each(_.zip(fields, d), function (x) {
obj[x[0]] = x[1];
});
return obj;
});
dfd.resolve(this._docsToQueryResult(objs));
@ -101,8 +104,8 @@ this.recline.Backend = this.recline.Backend || {};
if (gdocsSpreadsheet.feed.entry.length > 0) {
for (var k in gdocsSpreadsheet.feed.entry[0]) {
if (k.substr(0, 3) == 'gsx') {
var col = k.substr(4)
results.field.push(col);
var col = k.substr(4);
results.field.push(col);
}
}
}

View File

@ -15,7 +15,7 @@ this.recline.Backend = this.recline.Backend || {};
};
reader.onerror = function (e) {
alert('Failed to load file. Code: ' + e.target.error.code);
}
};
reader.readAsText(file);
};
@ -33,7 +33,7 @@ this.recline.Backend = this.recline.Backend || {};
});
var dataset = recline.Backend.createDataset(data, fields);
return dataset;
}
};
// Converts a Comma Separated Values string into an array of arrays.
// Each line in the CSV becomes an array.

View File

@ -15,7 +15,7 @@ this.recline.Backend = this.recline.Backend || {};
// If not defined (or id not provided) id will be autogenerated.
my.createDataset = function(data, fields, metadata) {
if (!metadata) {
var metadata = {};
metadata = {};
}
if (!metadata.id) {
metadata.id = String(Math.floor(Math.random() * 100000000) + 1);
@ -78,8 +78,8 @@ this.recline.Backend = this.recline.Backend || {};
},
sync: function(method, model, options) {
var self = this;
var dfd = $.Deferred();
if (method === "read") {
var dfd = $.Deferred();
if (model.__type__ == 'Dataset') {
var rawDataset = this.datasets[model.id];
model.set(rawDataset.metadata);
@ -89,7 +89,6 @@ this.recline.Backend = this.recline.Backend || {};
}
return dfd.promise();
} else if (method === 'update') {
var dfd = $.Deferred();
if (model.__type__ == 'Document') {
_.each(self.datasets[model.dataset.id].documents, function(doc, idx) {
if(doc.id === model.id) {
@ -100,7 +99,6 @@ this.recline.Backend = this.recline.Backend || {};
}
return dfd.promise();
} else if (method === 'delete') {
var dfd = $.Deferred();
if (model.__type__ == 'Document') {
var rawDataset = self.datasets[model.dataset.id];
var newdocs = _.reject(rawDataset.documents, function(doc) {