[#78,jshint][s]: cleaning up backends as per jshint instructions.
This commit is contained in:
@@ -12,7 +12,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
// Override Backbone.sync to hand off to sync function in relevant backend
|
// Override Backbone.sync to hand off to sync function in relevant backend
|
||||||
Backbone.sync = function(method, model, options) {
|
Backbone.sync = function(method, model, options) {
|
||||||
return model.backend.sync(method, model, options);
|
return model.backend.sync(method, model, options);
|
||||||
}
|
};
|
||||||
|
|
||||||
// ## recline.Backend.Base
|
// ## recline.Backend.Base
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -38,14 +38,14 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
var self = this;
|
var self = this;
|
||||||
var base = this.get('dataproxy_url');
|
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: base,
|
||||||
, data: data
|
data: data,
|
||||||
, dataType: 'jsonp'
|
dataType: 'jsonp'
|
||||||
});
|
});
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
this._wrapInTimeout(jqxhr).done(function(results) {
|
this._wrapInTimeout(jqxhr).done(function(results) {
|
||||||
|
|||||||
@@ -59,37 +59,33 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_normalizeQuery: function(queryObj) {
|
_normalizeQuery: function(queryObj) {
|
||||||
if (queryObj.toJSON) {
|
var out = queryObj.toJSON ? queryObj.toJSON() : _.extend({}, queryObj);
|
||||||
var out = queryObj.toJSON();
|
if (out.q !== undefined && out.q.trim() === '') {
|
||||||
} else {
|
|
||||||
var out = _.extend({}, queryObj);
|
|
||||||
}
|
|
||||||
if (out.q != undefined && out.q.trim() === '') {
|
|
||||||
delete out.q;
|
delete out.q;
|
||||||
}
|
}
|
||||||
if (!out.q) {
|
if (!out.q) {
|
||||||
out.query = {
|
out.query = {
|
||||||
match_all: {}
|
match_all: {}
|
||||||
}
|
};
|
||||||
} else {
|
} else {
|
||||||
out.query = {
|
out.query = {
|
||||||
query_string: {
|
query_string: {
|
||||||
query: out.q
|
query: out.q
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
delete out.q;
|
delete out.q;
|
||||||
}
|
}
|
||||||
// now do filters (note the *plural*)
|
// now do filters (note the *plural*)
|
||||||
if (out.filters && out.filters.length) {
|
if (out.filters && out.filters.length) {
|
||||||
if (!out.filter) {
|
if (!out.filter) {
|
||||||
out.filter = {}
|
out.filter = {};
|
||||||
}
|
}
|
||||||
if (!out.filter.and) {
|
if (!out.filter.and) {
|
||||||
out.filter.and = [];
|
out.filter.and = [];
|
||||||
}
|
}
|
||||||
out.filter.and = out.filter.and.concat(out.filters);
|
out.filter.and = out.filter.and.concat(out.filters);
|
||||||
}
|
}
|
||||||
if (out.filters != undefined) {
|
if (out.filters !== undefined) {
|
||||||
delete out.filters;
|
delete out.filters;
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
@@ -107,10 +103,10 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
// TODO: fail case
|
// TODO: fail case
|
||||||
jqxhr.done(function(results) {
|
jqxhr.done(function(results) {
|
||||||
_.each(results.hits.hits, function(hit) {
|
_.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;
|
hit._source.id = hit._id;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
if (results.facets) {
|
if (results.facets) {
|
||||||
results.hits.facets = results.facets;
|
results.hits.facets = results.facets;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
return url;
|
return url;
|
||||||
} else {
|
} else {
|
||||||
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0
|
// 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);
|
var matches = url.match(regex);
|
||||||
if (matches) {
|
if (matches) {
|
||||||
var key = matches[1];
|
var key = matches[1];
|
||||||
var worksheet = 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;
|
return out;
|
||||||
} else {
|
} else {
|
||||||
alert('Failed to extract gdocs key from ' + url);
|
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!)
|
// cache data onto dataset (we have loaded whole gdoc it seems!)
|
||||||
model._dataCache = result.data;
|
model._dataCache = result.data;
|
||||||
dfd.resolve(model);
|
dfd.resolve(model);
|
||||||
})
|
});
|
||||||
return dfd.promise(); }
|
return dfd.promise();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
query: function(dataset, queryObj) {
|
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
|
// TODO: factor this out as a common method with other backends
|
||||||
var objs = _.map(dataset._dataCache, function (d) {
|
var objs = _.map(dataset._dataCache, function (d) {
|
||||||
var obj = {};
|
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;
|
return obj;
|
||||||
});
|
});
|
||||||
dfd.resolve(this._docsToQueryResult(objs));
|
dfd.resolve(this._docsToQueryResult(objs));
|
||||||
@@ -101,8 +104,8 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
if (gdocsSpreadsheet.feed.entry.length > 0) {
|
if (gdocsSpreadsheet.feed.entry.length > 0) {
|
||||||
for (var k in gdocsSpreadsheet.feed.entry[0]) {
|
for (var k in gdocsSpreadsheet.feed.entry[0]) {
|
||||||
if (k.substr(0, 3) == 'gsx') {
|
if (k.substr(0, 3) == 'gsx') {
|
||||||
var col = k.substr(4)
|
var col = k.substr(4);
|
||||||
results.field.push(col);
|
results.field.push(col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
};
|
};
|
||||||
reader.onerror = function (e) {
|
reader.onerror = function (e) {
|
||||||
alert('Failed to load file. Code: ' + e.target.error.code);
|
alert('Failed to load file. Code: ' + e.target.error.code);
|
||||||
}
|
};
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
});
|
});
|
||||||
var dataset = recline.Backend.createDataset(data, fields);
|
var dataset = recline.Backend.createDataset(data, fields);
|
||||||
return dataset;
|
return dataset;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Converts a Comma Separated Values string into an array of arrays.
|
// Converts a Comma Separated Values string into an array of arrays.
|
||||||
// Each line in the CSV becomes an array.
|
// Each line in the CSV becomes an array.
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
// If not defined (or id not provided) id will be autogenerated.
|
// If not defined (or id not provided) id will be autogenerated.
|
||||||
my.createDataset = function(data, fields, metadata) {
|
my.createDataset = function(data, fields, metadata) {
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
var metadata = {};
|
metadata = {};
|
||||||
}
|
}
|
||||||
if (!metadata.id) {
|
if (!metadata.id) {
|
||||||
metadata.id = String(Math.floor(Math.random() * 100000000) + 1);
|
metadata.id = String(Math.floor(Math.random() * 100000000) + 1);
|
||||||
@@ -78,8 +78,8 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
},
|
},
|
||||||
sync: function(method, model, options) {
|
sync: function(method, model, options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var dfd = $.Deferred();
|
||||||
if (method === "read") {
|
if (method === "read") {
|
||||||
var dfd = $.Deferred();
|
|
||||||
if (model.__type__ == 'Dataset') {
|
if (model.__type__ == 'Dataset') {
|
||||||
var rawDataset = this.datasets[model.id];
|
var rawDataset = this.datasets[model.id];
|
||||||
model.set(rawDataset.metadata);
|
model.set(rawDataset.metadata);
|
||||||
@@ -89,7 +89,6 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
}
|
}
|
||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
} else if (method === 'update') {
|
} else if (method === 'update') {
|
||||||
var dfd = $.Deferred();
|
|
||||||
if (model.__type__ == 'Document') {
|
if (model.__type__ == 'Document') {
|
||||||
_.each(self.datasets[model.dataset.id].documents, function(doc, idx) {
|
_.each(self.datasets[model.dataset.id].documents, function(doc, idx) {
|
||||||
if(doc.id === model.id) {
|
if(doc.id === model.id) {
|
||||||
@@ -100,7 +99,6 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
}
|
}
|
||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
} else if (method === 'delete') {
|
} else if (method === 'delete') {
|
||||||
var dfd = $.Deferred();
|
|
||||||
if (model.__type__ == 'Document') {
|
if (model.__type__ == 'Document') {
|
||||||
var rawDataset = self.datasets[model.dataset.id];
|
var rawDataset = self.datasets[model.dataset.id];
|
||||||
var newdocs = _.reject(rawDataset.documents, function(doc) {
|
var newdocs = _.reject(rawDataset.documents, function(doc) {
|
||||||
|
|||||||
Reference in New Issue
Block a user