From 1491ae5bcc05cd2d832f8ac794345a627a21b735 Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Thu, 9 Feb 2012 18:05:37 +0000 Subject: [PATCH] [#34,query][m]: start on proper query support (move to query and sort support in BackendMemory). --- src/backend.js | 50 +++++++++++++++++++++------------------------- src/model.js | 11 ++++++++-- src/view.js | 10 ++++++++-- test/model.test.js | 30 +++++++++++++++++++--------- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/backend.js b/src/backend.js index 7fae84b6..581311ec 100644 --- a/src/backend.js +++ b/src/backend.js @@ -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'); diff --git a/src/model.js b/src/model.js index 9eccc2f1..5ff36653 100644 --- a/src/model.js +++ b/src/model.js @@ -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; diff --git a/src/view.js b/src/view.js index 489879ff..d1850d7d 100644 --- a/src/view.js +++ b/src/view.js @@ -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() { diff --git a/test/model.test.js b/test/model.test.js index 2e5f4415..587142be 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -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() { @@ -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')); }); @@ -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 @@ -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'));