[model,view][s]: (refs #10) refactor to return DocumentList from Dataset.getRows rather than array of simple hashes.
This commit is contained in:
parent
7e67a145f4
commit
d7e058eb15
15
src/model.js
15
src/model.js
@ -6,10 +6,23 @@ recline.Dataset = Backbone.Model.extend({
|
||||
getLength: function() {
|
||||
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 also illustrates the limitations of separating the Dataset and the Backend
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
20
src/view.js
20
src/view.js
@ -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({
|
||||
tagName: "div",
|
||||
className: "data-table-container",
|
||||
@ -69,8 +72,8 @@ recline.DataTable = Backbone.View.extend({
|
||||
initialize: function() {
|
||||
this.el = $(this.el);
|
||||
var self = this;
|
||||
this.model.getRows().then(function(rows) {
|
||||
self._currentRows = rows;
|
||||
this.model.getRows().then(function(documentList) {
|
||||
self._currentDocuments = documentList;
|
||||
self.render()
|
||||
});
|
||||
this.state = {};
|
||||
@ -199,9 +202,9 @@ recline.DataTable = Backbone.View.extend({
|
||||
|
||||
toTemplateJSON: function() {
|
||||
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) {
|
||||
return {header: header, value: row[header]}
|
||||
return {header: header, value: doc.get(header)}
|
||||
})
|
||||
return { id: 'xxx', cells: cellData }
|
||||
})
|
||||
@ -280,8 +283,8 @@ recline.FlotGraph = Backbone.View.extend({
|
||||
graphType: 'line'
|
||||
};
|
||||
var self = this;
|
||||
this.model.getRows().then(function(rows) {
|
||||
self._currentRows = rows;
|
||||
this.model.getRows().then(function(documentList) {
|
||||
self._currentDocuments = documentList;
|
||||
self.render()
|
||||
});
|
||||
},
|
||||
@ -342,8 +345,9 @@ recline.FlotGraph = Backbone.View.extend({
|
||||
if (this.chartConfig) {
|
||||
$.each(this.chartConfig.series, function (seriesIndex, field) {
|
||||
var points = [];
|
||||
$.each(self._currentRows, function (index) {
|
||||
var x = this[self.chartConfig.group], y = this[field];
|
||||
$.each(self._currentDocuments.models, function (index, doc) {
|
||||
var x = doc.get(self.chartConfig.group);
|
||||
var y = doc.get(field);
|
||||
if (typeof x === 'string') {
|
||||
x = index;
|
||||
}
|
||||
|
||||
@ -33,12 +33,12 @@ test('new Dataset', function () {
|
||||
equal(dataset.get('name'), metadata.name);
|
||||
equal(dataset.get('headers'), indata.headers);
|
||||
equal(dataset.getLength(), 6);
|
||||
dataset.getRows(4, 2).then(function(rows) {
|
||||
equal(rows[0], indata.rows[2]);
|
||||
dataset.getRows(4, 2).then(function(documentList) {
|
||||
deepEqual(indata.rows[2], documentList.models[0].toJSON());
|
||||
});
|
||||
dataset.getRows().then(function(rows) {
|
||||
equal(rows.length, Math.min(10, indata.rows.length));
|
||||
equal(rows[0], indata.rows[0]);
|
||||
dataset.getRows().then(function(docList) {
|
||||
equal(docList.length, Math.min(10, indata.rows.length));
|
||||
deepEqual(docList.models[0].toJSON(), indata.rows[0]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -136,14 +136,14 @@ test('Webstore Backend', function() {
|
||||
});
|
||||
|
||||
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)
|
||||
// restore mocked method
|
||||
$.ajax.restore();
|
||||
dataset.getRows().then(function(rows) {
|
||||
dataset.getRows().then(function(docList) {
|
||||
start();
|
||||
equal(3,rows.length)
|
||||
equal("2009-01-01", rows[0].date);
|
||||
equal(3, docList.length)
|
||||
equal("2009-01-01", docList.models[0].get('date'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user