[#128,backend/gdocs][s]: minor refactoring to make cleaner and have it actually work.

This commit is contained in:
Rufus Pollock 2012-05-26 18:07:36 +01:00
parent e9d1b8a55a
commit feaaf78639
2 changed files with 22 additions and 20 deletions

View File

@ -19,6 +19,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
// );
// </pre>
my.Backbone = function() {
var self = this;
this.__type__ = 'gdocs';
this.readonly = true;
@ -26,23 +27,31 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
var self = this;
if (method === "read") {
var dfd = $.Deferred();
loadData(model.get('url')).done(function(result) {
model.fields.reset(result.fields);
// cache data onto dataset (we have loaded whole gdoc it seems!)
model._dataCache = result.data;
dfd.resolve(model);
});
dfd.resolve(model);
return dfd.promise();
}
};
this.query = function(dataset, queryObj) {
var dfd = $.Deferred();
var fields = _.pluck(dataset.fields.toJSON(), 'id');
if (dataset._dataCache) {
dfd.resolve(dataset._dataCache);
} else {
loadData(dataset.get('url')).done(function(result) {
dataset.fields.reset(result.fields);
// cache data onto dataset (we have loaded whole gdoc it seems!)
dataset._dataCache = self._formatResults(dataset, result.data);
dfd.resolve(dataset._dataCache);
});
}
return dfd.promise();
};
this._formatResults = function(dataset, data) {
var fields = _.pluck(dataset.fields.toJSON(), 'id');
// zip the fields with the data rows to produce js objs
// TODO: factor this out as a common method with other backends
var objs = _.map(dataset._dataCache, function (d) {
var objs = _.map(data, function (d) {
var obj = {};
_.each(_.zip(fields, d), function (x) {
obj[x[0]] = x[1];
@ -55,8 +64,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
return { _source: row }
})
}
dfd.resolve(out);
return dfd;
return out;
};
};
@ -78,7 +86,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
$.getJSON(url, function(d) {
result = my.parseData(d);
result.fields = _.map(result.fields, function(fieldId) {
return {id: fieldId};
return {id: fieldId};
});
dfd.resolve(result);
});

View File

@ -68,7 +68,6 @@ test('DataProxy Backend', function() {
// needed only if not stubbing
// stop();
var backend = new recline.Backend.DataProxy.Backbone();
console.log(backend.readonly);
ok(backend.readonly);
equal(backend.__type__, 'dataproxy');
@ -288,15 +287,10 @@ test("GDocs Backend", function() {
}
});
dataset.fetch().then(function(dataset) {
dataset.query().then(function(docList) {
deepEqual(['column-2', 'column-1'], _.pluck(dataset.fields.toJSON(), 'id'));
//equal(null, dataset.docCount)
dataset.query().then(function(docList) {
equal(3, docList.length);
equal("A", docList.models[0].get('column-1'));
// needed only if not stubbing
start();
});
equal(3, docList.length);
equal("A", docList.models[0].get('column-1'));
});
$.getJSON.restore();
});