[#34,query][m]: start on proper query support (move to query and sort support in BackendMemory).

This commit is contained in:
Rufus Pollock
2012-02-09 18:05:37 +00:00
parent 0af2394eeb
commit 1491ae5bcc
4 changed files with 61 additions and 40 deletions

View File

@@ -78,16 +78,19 @@ this.recline.Model = this.recline.Model || {};
alert('Not supported: sync on BackendMemory with method ' + method + ' and model ' + model);
}
},
getDocuments: function(model, numRows, start) {
if (start === undefined) {
start = 0;
}
if (numRows === undefined) {
numRows = 10;
}
query: function(model, queryObj) {
var numRows = queryObj.size;
var start = queryObj.offset;
var dfd = $.Deferred();
rows = model.backendConfig.data.rows;
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);
return dfd.promise();
}
@@ -133,19 +136,18 @@ this.recline.Model = this.recline.Model || {};
}
}
},
getDocuments: function(model, numRows, start) {
if (start === undefined) {
start = 0;
}
if (numRows === undefined) {
numRows = 10;
}
query: function(model, queryObj) {
var base = model.backendConfig.url;
var data = {
_limit: queryObj.size
, _offset: queryObj.offset
};
var jqxhr = $.ajax({
url: base + '.json?_limit=' + numRows,
dataType: 'jsonp',
jsonp: '_callback',
cache: true
url: base + '.json',
data: data,
dataType: 'jsonp',
jsonp: '_callback',
cache: true
});
var dfd = $.Deferred();
jqxhr.then(function(results) {
@@ -206,17 +208,11 @@ this.recline.Model = this.recline.Model || {};
alert('This backend only supports read operations');
}
},
getDocuments: function(dataset, numRows, start) {
if (start === undefined) {
start = 0;
}
if (numRows === undefined) {
numRows = 10;
}
query: function(dataset, queryObj) {
var base = my.backends['dataproxy'].get('dataproxy');
var data = {
url: dataset.backendConfig.url
, 'max-results': numRows
, 'max-results': queryObj.size
, type: dataset.backendConfig.format
};
var jqxhr = $.ajax({
@@ -260,7 +256,7 @@ this.recline.Model = this.recline.Model || {};
return dfd.promise(); }
},
getDocuments: function(dataset, start, numRows) {
query: function(dataset, queryObj) {
var dfd = $.Deferred();
var fields = dataset.get('headers');

View File

@@ -15,6 +15,11 @@ this.recline.Model = this.recline.Model || {};
this.currentDocuments = new my.DocumentList();
this.docCount = null;
this.backend = null;
this.defaultQuery = {
size: 100
, offset: 0
};
// this.queryState = {};
},
// ### 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 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 backend = my.backends[this.backendConfig.type];
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 _doc = new my.Document(row);
_doc.backendConfig = self.backendConfig;

View File

@@ -104,14 +104,20 @@ my.DataExplorer = Backbone.View.extend({
this.model.fetch().then(function(dataset) {
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
// initialize of dataTable calls render
self.model.getDocuments(self.config.displayCount);
var queryObj = {
size: self.config.displayCount
};
self.model.query(queryObj);
});
},
onDisplayCountUpdate: function(e) {
e.preventDefault();
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() {