[#49,query][s]: introduce Query model object and use it in Dataset.
* tests: refactor Memory backend tests so that tests are independent (to avoid issues of carry over of query state) * TODO: use query stuff in views - NB sorting seems broken in DataTable.
This commit is contained in:
33
src/model.js
33
src/model.js
@@ -13,37 +13,31 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
my.Dataset = Backbone.Model.extend({
|
my.Dataset = Backbone.Model.extend({
|
||||||
__type__: 'Dataset',
|
__type__: 'Dataset',
|
||||||
initialize: function(model, backend) {
|
initialize: function(model, backend) {
|
||||||
|
_.bindAll(this, 'query');
|
||||||
this.backend = backend;
|
this.backend = backend;
|
||||||
if (backend && backend.constructor == String) {
|
if (backend && backend.constructor == String) {
|
||||||
this.backend = my.backends[backend];
|
this.backend = my.backends[backend];
|
||||||
}
|
}
|
||||||
this.currentDocuments = new my.DocumentList();
|
this.currentDocuments = new my.DocumentList();
|
||||||
this.docCount = null;
|
this.docCount = null;
|
||||||
this.defaultQuery = {
|
this.queryState = new my.Query();
|
||||||
size: 100
|
this.queryState.bind('change', this.query);
|
||||||
, offset: 0
|
|
||||||
};
|
|
||||||
// this.queryState = {};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// ### getDocuments
|
// ### query
|
||||||
//
|
//
|
||||||
// AJAX method with promise API to get rows (documents) from the backend.
|
// AJAX method with promise API to get documents from the backend.
|
||||||
|
//
|
||||||
|
// It will query based on current query state (given by this.queryState)
|
||||||
|
// updated by queryObj (if provided).
|
||||||
//
|
//
|
||||||
// Resulting DocumentList are used to reset this.currentDocuments and are
|
// Resulting DocumentList are used to reset this.currentDocuments and are
|
||||||
// also returned.
|
// also returned.
|
||||||
//
|
|
||||||
// :param numRows: passed onto backend getDocuments.
|
|
||||||
// :param start: passed onto backend 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
|
|
||||||
query: function(queryObj) {
|
query: function(queryObj) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.queryState = queryObj || this.defaultQuery;
|
this.queryState.set(queryObj, {silent: true});
|
||||||
this.queryState = _.extend({size: 100, offset: 0}, this.queryState);
|
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
this.backend.query(this, this.queryState).done(function(rows) {
|
this.backend.query(this, this.queryState.toJSON()).done(function(rows) {
|
||||||
var docs = _.map(rows, function(row) {
|
var docs = _.map(rows, function(row) {
|
||||||
var _doc = new my.Document(row);
|
var _doc = new my.Document(row);
|
||||||
_doc.backend = self.backend;
|
_doc.backend = self.backend;
|
||||||
@@ -79,6 +73,13 @@ my.DocumentList = Backbone.Collection.extend({
|
|||||||
model: my.Document
|
model: my.Document
|
||||||
});
|
});
|
||||||
|
|
||||||
|
my.Query = Backbone.Model.extend({
|
||||||
|
defaults: {
|
||||||
|
size: 100
|
||||||
|
, offset: 0
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// ## Backend registry
|
// ## Backend registry
|
||||||
//
|
//
|
||||||
// Backends will register themselves by id into this registry
|
// Backends will register themselves by id into this registry
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
(function ($) {
|
(function ($) {
|
||||||
module("Dataset");
|
module("Dataset");
|
||||||
|
|
||||||
test('Memory Backend', function () {
|
var memoryData = {
|
||||||
var datasetId = 'test-dataset';
|
|
||||||
var inData = {
|
|
||||||
metadata: {
|
metadata: {
|
||||||
title: 'My Test Dataset'
|
title: 'My Test Dataset'
|
||||||
, name: '1-my-test-dataset'
|
, name: '1-my-test-dataset'
|
||||||
, id: datasetId
|
, id: 'test-dataset'
|
||||||
, headers: ['x', 'y', 'z']
|
, headers: ['x', 'y', 'z']
|
||||||
},
|
},
|
||||||
documents: [
|
documents: [
|
||||||
@@ -19,17 +17,31 @@ test('Memory Backend', function () {
|
|||||||
, {id: 5, x: 6, y: 12, z: 18}
|
, {id: 5, x: 6, y: 12, z: 18}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function makeBackendDataset() {
|
||||||
var backend = new recline.Model.BackendMemory();
|
var backend = new recline.Model.BackendMemory();
|
||||||
backend.addDataset(inData);
|
backend.addDataset(memoryData);
|
||||||
var dataset = new recline.Model.Dataset({id: datasetId}, backend);
|
var dataset = new recline.Model.Dataset({id: memoryData.metadata.id}, backend);
|
||||||
// ### Start testing
|
return dataset;
|
||||||
expect(10);
|
}
|
||||||
// convenience for tests
|
|
||||||
var data = backend.datasets[datasetId];
|
test('Memory Backend: basics', function () {
|
||||||
|
var dataset = makeBackendDataset();
|
||||||
|
expect(3);
|
||||||
|
// convenience for tests - get the data that should get changed
|
||||||
|
var data = dataset.backend.datasets[memoryData.metadata.id];
|
||||||
dataset.fetch().then(function(datasetAgain) {
|
dataset.fetch().then(function(datasetAgain) {
|
||||||
equal(dataset.get('name'), data.metadata.name);
|
equal(dataset.get('name'), data.metadata.name);
|
||||||
deepEqual(dataset.get('headers'), data.metadata.headers);
|
deepEqual(dataset.get('headers'), data.metadata.headers);
|
||||||
equal(dataset.docCount, 6);
|
equal(dataset.docCount, 6);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Memory Backend: query', function () {
|
||||||
|
var dataset = makeBackendDataset();
|
||||||
|
// convenience for tests - get the data that should get changed
|
||||||
|
var data = dataset.backend.datasets[memoryData.metadata.id];
|
||||||
|
var dataset = makeBackendDataset();
|
||||||
var queryObj = {
|
var queryObj = {
|
||||||
size: 4
|
size: 4
|
||||||
, offset: 2
|
, offset: 2
|
||||||
@@ -37,6 +49,12 @@ test('Memory Backend', function () {
|
|||||||
dataset.query(queryObj).then(function(documentList) {
|
dataset.query(queryObj).then(function(documentList) {
|
||||||
deepEqual(data.documents[2], documentList.models[0].toJSON());
|
deepEqual(data.documents[2], documentList.models[0].toJSON());
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Memory Backend: query sort', function () {
|
||||||
|
var dataset = makeBackendDataset();
|
||||||
|
// convenience for tests - get the data that should get changed
|
||||||
|
var data = dataset.backend.datasets[memoryData.metadata.id];
|
||||||
var queryObj = {
|
var queryObj = {
|
||||||
sort: [
|
sort: [
|
||||||
['y', 'desc']
|
['y', 'desc']
|
||||||
@@ -46,6 +64,12 @@ test('Memory Backend', function () {
|
|||||||
var doc0 = dataset.currentDocuments.models[0].toJSON();
|
var doc0 = dataset.currentDocuments.models[0].toJSON();
|
||||||
equal(doc0.x, 6);
|
equal(doc0.x, 6);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Memory Backend: update and delete', function () {
|
||||||
|
var dataset = makeBackendDataset();
|
||||||
|
// convenience for tests - get the data that should get changed
|
||||||
|
var data = dataset.backend.datasets[memoryData.metadata.id];
|
||||||
dataset.query().then(function(docList) {
|
dataset.query().then(function(docList) {
|
||||||
equal(docList.length, Math.min(100, data.documents.length));
|
equal(docList.length, Math.min(100, data.documents.length));
|
||||||
var doc1 = docList.models[0];
|
var doc1 = docList.models[0];
|
||||||
@@ -61,8 +85,7 @@ test('Memory Backend', function () {
|
|||||||
// Test Delete
|
// Test Delete
|
||||||
doc1.destroy().then(function() {
|
doc1.destroy().then(function() {
|
||||||
equal(data.documents.length, 5);
|
equal(data.documents.length, 5);
|
||||||
equal(data.documents[0].x, inData.documents[1].x);
|
equal(data.documents[0].x, memoryData.documents[1].x);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user