[model/backend][s]: BackendMemory now works with one and only one dataset at a time (plus document what dataset data looks like).

* It is clear that if Backend is to handle syncing of Documents we need to only be working with one dataset (or: every Document has to reference a dataset -- in which case Backend and Dataset really are just one object ...).
This commit is contained in:
Rufus Pollock
2012-01-05 01:00:33 +00:00
parent 51934e1b94
commit 164da57dfa
3 changed files with 30 additions and 16 deletions

View File

@@ -36,8 +36,7 @@ function demoDataset() {
] ]
}; };
// this is all rather artificial here but would make more sense with more complex backend // this is all rather artificial here but would make more sense with more complex backend
var backend = new recline.Model.BackendMemory(); var backend = new recline.Model.BackendMemory({
backend.addDataset({
metadata: metadata, metadata: metadata,
data: indata data: indata
}); });

View File

@@ -54,16 +54,33 @@ my.setBackend = function(backend) {
// //
// Does not need to be a backbone model but provides some conveience // Does not need to be a backbone model but provides some conveience
my.BackendMemory = Backbone.Model.extend({ my.BackendMemory = Backbone.Model.extend({
initialize: function() { // Initialize a Backend with a local in-memory dataset.
this._datasetCache = {} //
// NB: We can handle one and only one dataset at a time.
//
// :param dataset: the data for a dataset on which operations will be
// performed. In the form of a hash with metadata and data attributes.
// - metadata: hash of key/value attributes of any kind (but usually with title attribute)
// - data: hash with 2 keys:
// - headers: list of header names/labels
// - rows: list of hashes, each hash being one row. A row *must* have an id attribute which is unique.
//
// Example of data:
//
// {
// headers: ['x', 'y', 'z']
// , rows: [
// {id: 0, x: 1, y: 2, z: 3}
// , {id: 1, x: 2, y: 4, z: 6}
// ]
// };
initialize: function(dataset) {
// deep copy
this._datasetAsData = _.extend({}, dataset);
}, },
// dataset is object with metadata and data attributes getDataset: function() {
addDataset: function(dataset) {
this._datasetCache[dataset.metadata.id] = dataset;
},
getDataset: function(id) {
var dataset = new my.Dataset({ var dataset = new my.Dataset({
id: id id: this._datasetAsData.metadata.id
}); });
// this is a bit weird but problem is in sync this is set to parent model object so need to give dataset a reference to backend explicitly // this is a bit weird but problem is in sync this is set to parent model object so need to give dataset a reference to backend explicitly
dataset.backend = this; dataset.backend = this;
@@ -76,9 +93,8 @@ my.BackendMemory = Backbone.Model.extend({
// think may make more sense to do work in individual objects rather than in central Backbone.sync // think may make more sense to do work in individual objects rather than in central Backbone.sync
if (this.__type__ == 'Dataset') { if (this.__type__ == 'Dataset') {
var dataset = this; var dataset = this;
var rawDataset = this.backend._datasetCache[model.id]; var rawDataset = this.backend._datasetAsData;
dataset.set(rawDataset.metadata); dataset.set(rawDataset.metadata);
// here we munge it all onto Dataset
dataset.set({ dataset.set({
headers: rawDataset.data.headers headers: rawDataset.data.headers
}); });
@@ -96,7 +112,7 @@ my.BackendMemory = Backbone.Model.extend({
numRows = 10; numRows = 10;
} }
var dfd = $.Deferred(); var dfd = $.Deferred();
rows = this._datasetCache[datasetId].data.rows; rows = this._datasetAsData.data.rows;
var results = rows.slice(start, start+numRows); var results = rows.slice(start, start+numRows);
dfd.resolve(results); dfd.resolve(results);
return dfd.promise(); return dfd.promise();
@@ -105,7 +121,7 @@ my.BackendMemory = Backbone.Model.extend({
// Webstore Backend for connecting to the Webstore // Webstore Backend for connecting to the Webstore
// //
// Designed to only attached to only dataset and one dataset only ... // Designed to only attach to one dataset and one dataset only ...
// Could generalize to support attaching to different datasets // Could generalize to support attaching to different datasets
my.BackendWebstore = Backbone.Model.extend({ my.BackendWebstore = Backbone.Model.extend({
// require url attribute in initialization data // require url attribute in initialization data

View File

@@ -21,8 +21,7 @@ test('new Dataset', function () {
] ]
}; };
// this is all rather artificial here but would make more sense with more complex backend // this is all rather artificial here but would make more sense with more complex backend
backend = new recline.Model.BackendMemory(); backend = new recline.Model.BackendMemory({
backend.addDataset({
metadata: metadata, metadata: metadata,
data: indata data: indata
}); });