[#128,backend/gdocs][s]: minor refactoring to make cleaner and have it actually work.
This commit is contained in:
@@ -19,6 +19,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
// );
|
// );
|
||||||
// </pre>
|
// </pre>
|
||||||
my.Backbone = function() {
|
my.Backbone = function() {
|
||||||
|
var self = this;
|
||||||
this.__type__ = 'gdocs';
|
this.__type__ = 'gdocs';
|
||||||
this.readonly = true;
|
this.readonly = true;
|
||||||
|
|
||||||
@@ -26,23 +27,31 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
var self = this;
|
var self = this;
|
||||||
if (method === "read") {
|
if (method === "read") {
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
loadData(model.get('url')).done(function(result) {
|
dfd.resolve(model);
|
||||||
model.fields.reset(result.fields);
|
|
||||||
// cache data onto dataset (we have loaded whole gdoc it seems!)
|
|
||||||
model._dataCache = result.data;
|
|
||||||
dfd.resolve(model);
|
|
||||||
});
|
|
||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.query = function(dataset, queryObj) {
|
this.query = function(dataset, queryObj) {
|
||||||
var dfd = $.Deferred();
|
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
|
// zip the fields with the data rows to produce js objs
|
||||||
// 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(data, function (d) {
|
||||||
var obj = {};
|
var obj = {};
|
||||||
_.each(_.zip(fields, d), function (x) {
|
_.each(_.zip(fields, d), function (x) {
|
||||||
obj[x[0]] = x[1];
|
obj[x[0]] = x[1];
|
||||||
@@ -55,8 +64,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
return { _source: row }
|
return { _source: row }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
dfd.resolve(out);
|
return out;
|
||||||
return dfd;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,7 +86,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
$.getJSON(url, function(d) {
|
$.getJSON(url, function(d) {
|
||||||
result = my.parseData(d);
|
result = my.parseData(d);
|
||||||
result.fields = _.map(result.fields, function(fieldId) {
|
result.fields = _.map(result.fields, function(fieldId) {
|
||||||
return {id: fieldId};
|
return {id: fieldId};
|
||||||
});
|
});
|
||||||
dfd.resolve(result);
|
dfd.resolve(result);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ test('DataProxy Backend', function() {
|
|||||||
// needed only if not stubbing
|
// needed only if not stubbing
|
||||||
// stop();
|
// stop();
|
||||||
var backend = new recline.Backend.DataProxy.Backbone();
|
var backend = new recline.Backend.DataProxy.Backbone();
|
||||||
console.log(backend.readonly);
|
|
||||||
ok(backend.readonly);
|
ok(backend.readonly);
|
||||||
equal(backend.__type__, 'dataproxy');
|
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'));
|
deepEqual(['column-2', 'column-1'], _.pluck(dataset.fields.toJSON(), 'id'));
|
||||||
//equal(null, dataset.docCount)
|
equal(3, docList.length);
|
||||||
dataset.query().then(function(docList) {
|
equal("A", docList.models[0].get('column-1'));
|
||||||
equal(3, docList.length);
|
|
||||||
equal("A", docList.models[0].get('column-1'));
|
|
||||||
// needed only if not stubbing
|
|
||||||
start();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
$.getJSON.restore();
|
$.getJSON.restore();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user