[model][m]: (fixes #6) create a BackendMemory (local data in memory) and refactor code to use this rather than current hard-coding-of-data approach.
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
$(function() {
|
$(function() {
|
||||||
|
var datasetId = 'test-dataset';
|
||||||
var metadata = {
|
var metadata = {
|
||||||
title: 'My Test Dataset'
|
title: 'My Test Dataset'
|
||||||
, name: '1-my-test-dataset'
|
, name: '1-my-test-dataset'
|
||||||
, id: 1
|
, id: datasetId
|
||||||
};
|
};
|
||||||
var indata = {
|
var indata = {
|
||||||
headers: ['x', 'y', 'z']
|
headers: ['x', 'y', 'z']
|
||||||
@@ -15,14 +16,23 @@ $(function() {
|
|||||||
, {x: 6, y: 12, z: 18}
|
, {x: 6, y: 12, z: 18}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
var dataset = new recline.Dataset(metadata, indata);
|
// this is all rather artificial here but would make more sense with more complex backend
|
||||||
|
backend = new recline.BackendMemory();
|
||||||
|
backend.addDataset({
|
||||||
|
metadata: metadata,
|
||||||
|
data: indata
|
||||||
|
});
|
||||||
|
recline.setBackend(backend);
|
||||||
|
var dataset = backend.getDataset(datasetId);
|
||||||
|
dataset.fetch().then(function() {
|
||||||
|
console.log(dataset.documentSet);
|
||||||
var dataTable = new recline.DataTable({
|
var dataTable = new recline.DataTable({
|
||||||
model: dataset.documentSet,
|
model: dataset.documentSet,
|
||||||
url: "awesome.com/webstore.json"
|
url: "awesome.com/webstore.json"
|
||||||
})
|
})
|
||||||
|
|
||||||
$('.container').append(dataTable.el)
|
$('.container').append(dataTable.el)
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
// app.after = {
|
// app.after = {
|
||||||
|
|||||||
51
src/model.js
51
src/model.js
@@ -2,9 +2,7 @@ this.recline = this.recline || {};
|
|||||||
|
|
||||||
// A Dataset model.
|
// A Dataset model.
|
||||||
recline.Dataset = Backbone.Model.extend({
|
recline.Dataset = Backbone.Model.extend({
|
||||||
initialize: function(data, rawDocumentSet) {
|
__type__: 'Dataset'
|
||||||
this.documentSet = new recline.DocumentSet(rawDocumentSet);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
recline.Document = Backbone.Model.extend({});
|
recline.Document = Backbone.Model.extend({});
|
||||||
@@ -15,9 +13,7 @@ recline.DocumentList = Backbone.Collection.extend({
|
|||||||
})
|
})
|
||||||
|
|
||||||
recline.DocumentSet = Backbone.Model.extend({
|
recline.DocumentSet = Backbone.Model.extend({
|
||||||
fetch: function(options) {
|
__type__: 'DocumentSet',
|
||||||
options.success(this);
|
|
||||||
},
|
|
||||||
getLength: function() {
|
getLength: function() {
|
||||||
return this.get('rows').length;
|
return this.get('rows').length;
|
||||||
},
|
},
|
||||||
@@ -35,3 +31,46 @@ recline.DocumentSet = Backbone.Model.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Backend which just caches in memory
|
||||||
|
//
|
||||||
|
// Does not need to be a backbone model but provides some conveience
|
||||||
|
recline.BackendMemory = Backbone.Model.extend({
|
||||||
|
initialize: function() {
|
||||||
|
this._datasetCache = {}
|
||||||
|
},
|
||||||
|
// dataset is object with metadata and data attributes
|
||||||
|
addDataset: function(dataset) {
|
||||||
|
this._datasetCache[dataset.metadata.id] = dataset;
|
||||||
|
},
|
||||||
|
getDataset: function(id) {
|
||||||
|
var dataset = new recline.Dataset({
|
||||||
|
id: 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
|
||||||
|
dataset.backend = this;
|
||||||
|
return dataset;
|
||||||
|
},
|
||||||
|
sync: function(method, model, options) {
|
||||||
|
if (method === "read") {
|
||||||
|
var dfd = $.Deferred();
|
||||||
|
// this switching on object type is rather horrible
|
||||||
|
// think may make more sense to do work in individual objects rather than in central Backbone.sync
|
||||||
|
if (this.__type__ == 'Dataset') {
|
||||||
|
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;
|
||||||
|
dfd.resolve(dataset);
|
||||||
|
} else if (this.__type__ == 'DocumentSet') {
|
||||||
|
dfd.resolve(this);
|
||||||
|
}
|
||||||
|
return dfd.promise();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
recline.setBackend = function(backend) {
|
||||||
|
Backbone.sync = backend.sync;
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,8 @@ recline.DataTable = Backbone.View.extend({
|
|||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
this.model.fetch({
|
this.model.fetch().then(function() {
|
||||||
success: function(collection, resp) {
|
|
||||||
that.render()
|
that.render()
|
||||||
}
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
toTemplateJSON: function() {
|
toTemplateJSON: function() {
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
module("Dataset");
|
module("Dataset");
|
||||||
|
|
||||||
test('new Dataset', function () {
|
test('new Dataset', function () {
|
||||||
|
var datasetId = 'test-dataset';
|
||||||
var metadata = {
|
var metadata = {
|
||||||
title: 'My Test Dataset'
|
title: 'My Test Dataset'
|
||||||
, name: '1-my-test-dataset'
|
, name: '1-my-test-dataset'
|
||||||
, id: 1
|
, id: datasetId
|
||||||
};
|
};
|
||||||
var indata = {
|
var indata = {
|
||||||
headers: ['x', 'y', 'z']
|
headers: ['x', 'y', 'z']
|
||||||
@@ -19,12 +20,20 @@ test('new Dataset', function () {
|
|||||||
, {x: 6, y: 12, z: 18}
|
, {x: 6, y: 12, z: 18}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
var dataset = new recline.Dataset(metadata, indata);
|
// this is all rather artificial here but would make more sense with more complex backend
|
||||||
equal(dataset.get('name'), metadata.name);
|
backend = new recline.BackendMemory();
|
||||||
|
backend.addDataset({
|
||||||
|
metadata: metadata,
|
||||||
|
data: indata
|
||||||
|
});
|
||||||
|
recline.setBackend(backend);
|
||||||
|
var dataset = backend.getDataset(datasetId);
|
||||||
expect(6);
|
expect(6);
|
||||||
setTimeout(2);
|
dataset.fetch().then(function(dataset) {
|
||||||
dataset.documentSet.fetch({
|
equal(dataset.get('name'), metadata.name);
|
||||||
success: function(documentSet) {
|
dataset.documentSet.fetch().then(testDS);
|
||||||
|
});
|
||||||
|
function testDS(documentSet) {
|
||||||
equal(documentSet.get('headers'), indata.headers);
|
equal(documentSet.get('headers'), indata.headers);
|
||||||
equal(documentSet.getLength(), 6);
|
equal(documentSet.getLength(), 6);
|
||||||
documentSet.getRows(4, 2).then(function(rows) {
|
documentSet.getRows(4, 2).then(function(rows) {
|
||||||
@@ -36,6 +45,8 @@ test('new Dataset', function () {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Local Data Sync', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
})(this.jQuery);
|
})(this.jQuery);
|
||||||
|
|||||||
Reference in New Issue
Block a user