Skip to content

Commit

Permalink
[#34,query][m]: start on proper query support (move to query and sort…
Browse files Browse the repository at this point in the history
… support in BackendMemory).
  • Loading branch information
rufuspollock committed Feb 9, 2012
1 parent 0af2394 commit 1491ae5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
50 changes: 23 additions & 27 deletions src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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');

Expand Down
11 changes: 9 additions & 2 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
30 changes: 21 additions & 9 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@
// deep copy so we do not touch original data ...
, data: $.extend(true, {}, indata)
};
expect(9);
expect(10);
dataset.fetch().then(function(dataset) {
equal(dataset.get('name'), metadata.name);
deepEqual(dataset.get('headers'), indata.headers);
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());
});
dataset.getDocuments().then(function(docList) {
// Test getDocuments
equal(docList.length, Math.min(10, indata.rows.length));
var queryObj = {
sort: [
['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];
deepEqual(doc1.toJSON(), indata.rows[0]);

// Test UPDATA
// Test UPDATE
var newVal = 10;
doc1.set({x: newVal});
doc1.save().then(function() {
Expand Down Expand Up @@ -158,7 +170,7 @@
dataset.fetch().then(function(dataset) {
deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers'));
equal(3, dataset.docCount)
dataset.getDocuments().then(function(docList) {
dataset.query().then(function(docList) {
equal(3, docList.length)
equal("2009-01-01", docList.models[0].get('date'));
});
Expand Down Expand Up @@ -253,7 +265,7 @@
dataset.fetch().then(function(dataset) {
deepEqual(['__id__', 'date', 'price'], dataset.get('headers'));
equal(null, dataset.docCount)
dataset.getDocuments().then(function(docList) {
dataset.query().then(function(docList) {
equal(10, docList.length)
equal("1950-01", docList.models[0].get('date'));
// needed only if not stubbing
Expand Down Expand Up @@ -455,7 +467,7 @@
console.log('inside dataset:', dataset, dataset.get('headers'), dataset.get('data'));
deepEqual(['column-2', 'column-1'], dataset.get('headers'));
//equal(null, dataset.docCount)
dataset.getDocuments().then(function(docList) {
dataset.query().then(function(docList) {
equal(3, docList.length);
console.log(docList.models[0]);
equal("A", docList.models[0].get('column-1'));
Expand Down

0 comments on commit 1491ae5

Please sign in to comment.