[model,refactor][m]: (refs #6, refs #10) get rid of DocumentSet merging its functionality into Dataset.
* In addition fully remove remaining references in models (in getRows and getLength functions) to form of backend (this should have been in previous commit).
This commit is contained in:
parent
21d9a1c2dc
commit
3a48044a76
@ -25,9 +25,8 @@ $(function() {
|
||||
recline.setBackend(backend);
|
||||
var dataset = backend.getDataset(datasetId);
|
||||
dataset.fetch().then(function() {
|
||||
console.log(dataset.documentSet);
|
||||
var dataTable = new recline.DataTable({
|
||||
model: dataset.documentSet,
|
||||
model: dataset,
|
||||
url: "awesome.com/webstore.json"
|
||||
})
|
||||
|
||||
|
||||
53
src/model.js
53
src/model.js
@ -2,7 +2,15 @@ this.recline = this.recline || {};
|
||||
|
||||
// A Dataset model.
|
||||
recline.Dataset = Backbone.Model.extend({
|
||||
__type__: 'Dataset'
|
||||
__type__: 'Dataset',
|
||||
getLength: function() {
|
||||
return this.rowCount;
|
||||
},
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
|
||||
recline.Document = Backbone.Model.extend({});
|
||||
@ -12,25 +20,6 @@ recline.DocumentList = Backbone.Collection.extend({
|
||||
model: recline.Document
|
||||
})
|
||||
|
||||
recline.DocumentSet = Backbone.Model.extend({
|
||||
__type__: 'DocumentSet',
|
||||
getLength: function() {
|
||||
return this.get('rows').length;
|
||||
},
|
||||
getRows: function(numRows, start) {
|
||||
if (start === undefined) {
|
||||
start = 0;
|
||||
}
|
||||
if (numRows === undefined) {
|
||||
numRows = 10;
|
||||
}
|
||||
var dfd = $.Deferred();
|
||||
var results = this.get('rows').slice(start, start+numRows);
|
||||
dfd.resolve(results);
|
||||
return dfd.promise();
|
||||
}
|
||||
});
|
||||
|
||||
// Backend which just caches in memory
|
||||
//
|
||||
// Does not need to be a backbone model but provides some conveience
|
||||
@ -59,15 +48,29 @@ recline.BackendMemory = Backbone.Model.extend({
|
||||
var dataset = this;
|
||||
var rawDataset = this.backend._datasetCache[model.id];
|
||||
dataset.set(rawDataset.metadata);
|
||||
dataset.documentSet = new recline.DocumentSet(rawDataset.data);
|
||||
dataset.documentSet.dataset = dataset;
|
||||
// here we munge it all onto Dataset
|
||||
dataset.set({
|
||||
headers: rawDataset.data.headers
|
||||
});
|
||||
dataset.rowCount = rawDataset.data.rows.length;
|
||||
dfd.resolve(dataset);
|
||||
} else if (this.__type__ == 'DocumentSet') {
|
||||
dfd.resolve(this);
|
||||
}
|
||||
return dfd.promise();
|
||||
}
|
||||
}
|
||||
},
|
||||
getRows: function(datasetId, numRows, start) {
|
||||
if (start === undefined) {
|
||||
start = 0;
|
||||
}
|
||||
if (numRows === undefined) {
|
||||
numRows = 10;
|
||||
}
|
||||
var dfd = $.Deferred();
|
||||
rows = this._datasetCache[datasetId].data.rows;
|
||||
var results = rows.slice(start, start+numRows);
|
||||
dfd.resolve(results);
|
||||
return dfd.promise();
|
||||
}
|
||||
});
|
||||
|
||||
recline.setBackend = function(backend) {
|
||||
|
||||
@ -14,12 +14,15 @@ recline.DataTable = Backbone.View.extend({
|
||||
initialize: function() {
|
||||
var that = this;
|
||||
this.model.fetch().then(function() {
|
||||
that.render()
|
||||
that.model.getRows().then(function(rows) {
|
||||
that._currentRows = rows;
|
||||
that.render()
|
||||
});
|
||||
})
|
||||
},
|
||||
toTemplateJSON: function() {
|
||||
var modelData = this.model.toJSON()
|
||||
modelData.rows = _.map(modelData.rows, function(row) {
|
||||
modelData.rows = _.map(this._currentRows, function(row) {
|
||||
var cellData = _.map(modelData.headers, function(header) {
|
||||
return {header: header, value: row[header]}
|
||||
})
|
||||
|
||||
@ -31,19 +31,16 @@ test('new Dataset', function () {
|
||||
expect(6);
|
||||
dataset.fetch().then(function(dataset) {
|
||||
equal(dataset.get('name'), metadata.name);
|
||||
dataset.documentSet.fetch().then(testDS);
|
||||
});
|
||||
function testDS(documentSet) {
|
||||
equal(documentSet.get('headers'), indata.headers);
|
||||
equal(documentSet.getLength(), 6);
|
||||
documentSet.getRows(4, 2).then(function(rows) {
|
||||
equal(dataset.get('headers'), indata.headers);
|
||||
equal(dataset.getLength(), 6);
|
||||
dataset.getRows(4, 2).then(function(rows) {
|
||||
equal(rows[0], indata.rows[2]);
|
||||
});
|
||||
documentSet.getRows().then(function(rows) {
|
||||
dataset.getRows().then(function(rows) {
|
||||
equal(rows.length, Math.min(10, indata.rows.length));
|
||||
equal(rows[0], indata.rows[0]);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('Local Data Sync', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user