[#34,query][m]: start on proper query support (move to query and sort support in BackendMemory).
This commit is contained in:
@@ -78,16 +78,19 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
alert('Not supported: sync on BackendMemory with method ' + method + ' and model ' + model);
|
alert('Not supported: sync on BackendMemory with method ' + method + ' and model ' + model);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getDocuments: function(model, numRows, start) {
|
query: function(model, queryObj) {
|
||||||
if (start === undefined) {
|
var numRows = queryObj.size;
|
||||||
start = 0;
|
var start = queryObj.offset;
|
||||||
}
|
|
||||||
if (numRows === undefined) {
|
|
||||||
numRows = 10;
|
|
||||||
}
|
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
rows = model.backendConfig.data.rows;
|
rows = model.backendConfig.data.rows;
|
||||||
var results = rows.slice(start, start+numRows);
|
var results = rows.slice(start, start+numRows);
|
||||||
|
// not complete sorting!
|
||||||
|
_.each(queryObj.sort, function(item) {
|
||||||
|
results = _.sortBy(results, function(row) {
|
||||||
|
var _out = row[item[0]];
|
||||||
|
return (item[1] == 'asc') ? _out : -1*_out;
|
||||||
|
});
|
||||||
|
});
|
||||||
dfd.resolve(results);
|
dfd.resolve(results);
|
||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
}
|
}
|
||||||
@@ -133,16 +136,15 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getDocuments: function(model, numRows, start) {
|
query: function(model, queryObj) {
|
||||||
if (start === undefined) {
|
|
||||||
start = 0;
|
|
||||||
}
|
|
||||||
if (numRows === undefined) {
|
|
||||||
numRows = 10;
|
|
||||||
}
|
|
||||||
var base = model.backendConfig.url;
|
var base = model.backendConfig.url;
|
||||||
|
var data = {
|
||||||
|
_limit: queryObj.size
|
||||||
|
, _offset: queryObj.offset
|
||||||
|
};
|
||||||
var jqxhr = $.ajax({
|
var jqxhr = $.ajax({
|
||||||
url: base + '.json?_limit=' + numRows,
|
url: base + '.json',
|
||||||
|
data: data,
|
||||||
dataType: 'jsonp',
|
dataType: 'jsonp',
|
||||||
jsonp: '_callback',
|
jsonp: '_callback',
|
||||||
cache: true
|
cache: true
|
||||||
@@ -206,17 +208,11 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
alert('This backend only supports read operations');
|
alert('This backend only supports read operations');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getDocuments: function(dataset, numRows, start) {
|
query: function(dataset, queryObj) {
|
||||||
if (start === undefined) {
|
|
||||||
start = 0;
|
|
||||||
}
|
|
||||||
if (numRows === undefined) {
|
|
||||||
numRows = 10;
|
|
||||||
}
|
|
||||||
var base = my.backends['dataproxy'].get('dataproxy');
|
var base = my.backends['dataproxy'].get('dataproxy');
|
||||||
var data = {
|
var data = {
|
||||||
url: dataset.backendConfig.url
|
url: dataset.backendConfig.url
|
||||||
, 'max-results': numRows
|
, 'max-results': queryObj.size
|
||||||
, type: dataset.backendConfig.format
|
, type: dataset.backendConfig.format
|
||||||
};
|
};
|
||||||
var jqxhr = $.ajax({
|
var jqxhr = $.ajax({
|
||||||
@@ -260,7 +256,7 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
return dfd.promise(); }
|
return dfd.promise(); }
|
||||||
},
|
},
|
||||||
|
|
||||||
getDocuments: function(dataset, start, numRows) {
|
query: function(dataset, queryObj) {
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
var fields = dataset.get('headers');
|
var fields = dataset.get('headers');
|
||||||
|
|
||||||
|
|||||||
11
src/model.js
11
src/model.js
@@ -15,6 +15,11 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
this.currentDocuments = new my.DocumentList();
|
this.currentDocuments = new my.DocumentList();
|
||||||
this.docCount = null;
|
this.docCount = null;
|
||||||
this.backend = null;
|
this.backend = null;
|
||||||
|
this.defaultQuery = {
|
||||||
|
size: 100
|
||||||
|
, offset: 0
|
||||||
|
};
|
||||||
|
// this.queryState = {};
|
||||||
},
|
},
|
||||||
|
|
||||||
// ### getDocuments
|
// ### getDocuments
|
||||||
@@ -29,11 +34,13 @@ this.recline.Model = this.recline.Model || {};
|
|||||||
//
|
//
|
||||||
// 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 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
|
// This also illustrates the limitations of separating the Dataset and the Backend
|
||||||
getDocuments: function(numRows, start) {
|
query: function(queryObj) {
|
||||||
|
this.actualQuery = queryObj || this.defaultQuery;
|
||||||
|
this.actualQuery = _.extend({size: 100, offset: 0}, this.actualQuery);
|
||||||
var self = this;
|
var self = this;
|
||||||
var backend = my.backends[this.backendConfig.type];
|
var backend = my.backends[this.backendConfig.type];
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
backend.getDocuments(this, numRows, start).then(function(rows) {
|
backend.query(this, this.actualQuery).then(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.backendConfig = self.backendConfig;
|
_doc.backendConfig = self.backendConfig;
|
||||||
|
|||||||
10
src/view.js
10
src/view.js
@@ -104,14 +104,20 @@ my.DataExplorer = Backbone.View.extend({
|
|||||||
this.model.fetch().then(function(dataset) {
|
this.model.fetch().then(function(dataset) {
|
||||||
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
|
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
|
||||||
// initialize of dataTable calls render
|
// initialize of dataTable calls render
|
||||||
self.model.getDocuments(self.config.displayCount);
|
var queryObj = {
|
||||||
|
size: self.config.displayCount
|
||||||
|
};
|
||||||
|
self.model.query(queryObj);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
onDisplayCountUpdate: function(e) {
|
onDisplayCountUpdate: function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.config.displayCount = parseInt(this.el.find('input[name="displayCount"]').val());
|
this.config.displayCount = parseInt(this.el.find('input[name="displayCount"]').val());
|
||||||
this.model.getDocuments(this.config.displayCount);
|
var queryObj = {
|
||||||
|
size: this.config.displayCount
|
||||||
|
};
|
||||||
|
this.model.query(queryObj);
|
||||||
},
|
},
|
||||||
|
|
||||||
setReadOnly: function() {
|
setReadOnly: function() {
|
||||||
|
|||||||
@@ -26,21 +26,33 @@
|
|||||||
// deep copy so we do not touch original data ...
|
// deep copy so we do not touch original data ...
|
||||||
, data: $.extend(true, {}, indata)
|
, data: $.extend(true, {}, indata)
|
||||||
};
|
};
|
||||||
expect(9);
|
expect(10);
|
||||||
dataset.fetch().then(function(dataset) {
|
dataset.fetch().then(function(dataset) {
|
||||||
equal(dataset.get('name'), metadata.name);
|
equal(dataset.get('name'), metadata.name);
|
||||||
deepEqual(dataset.get('headers'), indata.headers);
|
deepEqual(dataset.get('headers'), indata.headers);
|
||||||
equal(dataset.docCount, 6);
|
equal(dataset.docCount, 6);
|
||||||
dataset.getDocuments(4, 2).then(function(documentList) {
|
var queryObj = {
|
||||||
|
size: 4
|
||||||
|
, offset: 2
|
||||||
|
};
|
||||||
|
dataset.query(queryObj).then(function(documentList) {
|
||||||
deepEqual(indata.rows[2], documentList.models[0].toJSON());
|
deepEqual(indata.rows[2], documentList.models[0].toJSON());
|
||||||
});
|
});
|
||||||
dataset.getDocuments().then(function(docList) {
|
var queryObj = {
|
||||||
// Test getDocuments
|
sort: [
|
||||||
equal(docList.length, Math.min(10, indata.rows.length));
|
['y', 'desc']
|
||||||
|
]
|
||||||
|
};
|
||||||
|
dataset.query(queryObj).then(function(docs) {
|
||||||
|
var doc0 = dataset.currentDocuments.models[0].toJSON();
|
||||||
|
equal(doc0.x, 6);
|
||||||
|
});
|
||||||
|
dataset.query().then(function(docList) {
|
||||||
|
equal(docList.length, Math.min(100, indata.rows.length));
|
||||||
var doc1 = docList.models[0];
|
var doc1 = docList.models[0];
|
||||||
deepEqual(doc1.toJSON(), indata.rows[0]);
|
deepEqual(doc1.toJSON(), indata.rows[0]);
|
||||||
|
|
||||||
// Test UPDATA
|
// Test UPDATE
|
||||||
var newVal = 10;
|
var newVal = 10;
|
||||||
doc1.set({x: newVal});
|
doc1.set({x: newVal});
|
||||||
doc1.save().then(function() {
|
doc1.save().then(function() {
|
||||||
@@ -158,7 +170,7 @@
|
|||||||
dataset.fetch().then(function(dataset) {
|
dataset.fetch().then(function(dataset) {
|
||||||
deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers'));
|
deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers'));
|
||||||
equal(3, dataset.docCount)
|
equal(3, dataset.docCount)
|
||||||
dataset.getDocuments().then(function(docList) {
|
dataset.query().then(function(docList) {
|
||||||
equal(3, docList.length)
|
equal(3, docList.length)
|
||||||
equal("2009-01-01", docList.models[0].get('date'));
|
equal("2009-01-01", docList.models[0].get('date'));
|
||||||
});
|
});
|
||||||
@@ -253,7 +265,7 @@
|
|||||||
dataset.fetch().then(function(dataset) {
|
dataset.fetch().then(function(dataset) {
|
||||||
deepEqual(['__id__', 'date', 'price'], dataset.get('headers'));
|
deepEqual(['__id__', 'date', 'price'], dataset.get('headers'));
|
||||||
equal(null, dataset.docCount)
|
equal(null, dataset.docCount)
|
||||||
dataset.getDocuments().then(function(docList) {
|
dataset.query().then(function(docList) {
|
||||||
equal(10, docList.length)
|
equal(10, docList.length)
|
||||||
equal("1950-01", docList.models[0].get('date'));
|
equal("1950-01", docList.models[0].get('date'));
|
||||||
// needed only if not stubbing
|
// needed only if not stubbing
|
||||||
@@ -455,7 +467,7 @@
|
|||||||
console.log('inside dataset:', dataset, dataset.get('headers'), dataset.get('data'));
|
console.log('inside dataset:', dataset, dataset.get('headers'), dataset.get('data'));
|
||||||
deepEqual(['column-2', 'column-1'], dataset.get('headers'));
|
deepEqual(['column-2', 'column-1'], dataset.get('headers'));
|
||||||
//equal(null, dataset.docCount)
|
//equal(null, dataset.docCount)
|
||||||
dataset.getDocuments().then(function(docList) {
|
dataset.query().then(function(docList) {
|
||||||
equal(3, docList.length);
|
equal(3, docList.length);
|
||||||
console.log(docList.models[0]);
|
console.log(docList.models[0]);
|
||||||
equal("A", docList.models[0].get('column-1'));
|
equal("A", docList.models[0].get('column-1'));
|
||||||
|
|||||||
Reference in New Issue
Block a user