[model,view][s]: (refs #10) refactor to return DocumentList from Dataset.getRows rather than array of simple hashes.

This commit is contained in:
rgrp
2011-12-04 03:36:13 +00:00
parent 7e67a145f4
commit d7e058eb15
3 changed files with 35 additions and 18 deletions

View File

@@ -6,10 +6,23 @@ recline.Dataset = Backbone.Model.extend({
getLength: function() { getLength: function() {
return this.rowCount; return this.rowCount;
}, },
// Get rows (documents) from the backend returning a recline.DocumentList
//
// TODO: ? rename to getDocuments?
//
// this does not fit very well with Backbone setup. Backbone really expects you to know the ids of objects your are fetching (which you do in classic RESTful ajax-y world). But this paradigm does not fill well with data set up we have here. // this does not fit very well with Backbone setup. Backbone really expects you to know the ids of objects your are fetching (which you do in classic RESTful ajax-y world). But this paradigm does not fill well with data set up we have here.
// This also illustrates the limitations of separating the Dataset and the Backend // This also illustrates the limitations of separating the Dataset and the Backend
getRows: function(numRows, start) { getRows: function(numRows, start) {
return this.backend.getRows(this.id, numRows, start); var dfd = $.Deferred();
this.backend.getRows(this.id, numRows, start).then(function(rows) {
var docs = _.map(rows, function(row) {
return new recline.Document(row);
});
var docList = new recline.DocumentList(docs);
dfd.resolve(docList);
});
return dfd.promise();
} }
}); });

View File

@@ -62,6 +62,9 @@ recline.DataExplorer = Backbone.View.extend({
} }
}); });
// DataTable provides a tabular view on a Dataset.
//
// Initialize it with a recline.Dataset object.
recline.DataTable = Backbone.View.extend({ recline.DataTable = Backbone.View.extend({
tagName: "div", tagName: "div",
className: "data-table-container", className: "data-table-container",
@@ -69,8 +72,8 @@ recline.DataTable = Backbone.View.extend({
initialize: function() { initialize: function() {
this.el = $(this.el); this.el = $(this.el);
var self = this; var self = this;
this.model.getRows().then(function(rows) { this.model.getRows().then(function(documentList) {
self._currentRows = rows; self._currentDocuments = documentList;
self.render() self.render()
}); });
this.state = {}; this.state = {};
@@ -199,9 +202,9 @@ recline.DataTable = Backbone.View.extend({
toTemplateJSON: function() { toTemplateJSON: function() {
var modelData = this.model.toJSON() var modelData = this.model.toJSON()
modelData.rows = _.map(this._currentRows, function(row) { modelData.rows = _.map(this._currentDocuments.models, function(doc) {
var cellData = _.map(modelData.headers, function(header) { var cellData = _.map(modelData.headers, function(header) {
return {header: header, value: row[header]} return {header: header, value: doc.get(header)}
}) })
return { id: 'xxx', cells: cellData } return { id: 'xxx', cells: cellData }
}) })
@@ -280,8 +283,8 @@ recline.FlotGraph = Backbone.View.extend({
graphType: 'line' graphType: 'line'
}; };
var self = this; var self = this;
this.model.getRows().then(function(rows) { this.model.getRows().then(function(documentList) {
self._currentRows = rows; self._currentDocuments = documentList;
self.render() self.render()
}); });
}, },
@@ -342,8 +345,9 @@ recline.FlotGraph = Backbone.View.extend({
if (this.chartConfig) { if (this.chartConfig) {
$.each(this.chartConfig.series, function (seriesIndex, field) { $.each(this.chartConfig.series, function (seriesIndex, field) {
var points = []; var points = [];
$.each(self._currentRows, function (index) { $.each(self._currentDocuments.models, function (index, doc) {
var x = this[self.chartConfig.group], y = this[field]; var x = doc.get(self.chartConfig.group);
var y = doc.get(field);
if (typeof x === 'string') { if (typeof x === 'string') {
x = index; x = index;
} }

View File

@@ -33,12 +33,12 @@ test('new Dataset', function () {
equal(dataset.get('name'), metadata.name); equal(dataset.get('name'), metadata.name);
equal(dataset.get('headers'), indata.headers); equal(dataset.get('headers'), indata.headers);
equal(dataset.getLength(), 6); equal(dataset.getLength(), 6);
dataset.getRows(4, 2).then(function(rows) { dataset.getRows(4, 2).then(function(documentList) {
equal(rows[0], indata.rows[2]); deepEqual(indata.rows[2], documentList.models[0].toJSON());
}); });
dataset.getRows().then(function(rows) { dataset.getRows().then(function(docList) {
equal(rows.length, Math.min(10, indata.rows.length)); equal(docList.length, Math.min(10, indata.rows.length));
equal(rows[0], indata.rows[0]); deepEqual(docList.models[0].toJSON(), indata.rows[0]);
}); });
}); });
}); });
@@ -136,14 +136,14 @@ test('Webstore Backend', function() {
}); });
dataset.fetch().then(function(dataset) { dataset.fetch().then(function(dataset) {
equal(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers')); deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers'));
equal(3, dataset.rowCount) equal(3, dataset.rowCount)
// restore mocked method // restore mocked method
$.ajax.restore(); $.ajax.restore();
dataset.getRows().then(function(rows) { dataset.getRows().then(function(docList) {
start(); start();
equal(3,rows.length) equal(3, docList.length)
equal("2009-01-01", rows[0].date); equal("2009-01-01", docList.models[0].get('date'));
}); });
}); });
}); });