diff --git a/recline.js b/recline.js index 8a4ee686..596ebcf8 100644 --- a/recline.js +++ b/recline.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); + results = model.backendConfig.data.rows; + // 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; + }); + }); + var results = results.slice(start, start+numRows); 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'); @@ -581,6 +577,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 @@ -595,11 +596,14 @@ 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.queryState = queryObj || this.defaultQuery; + this.queryState = _.extend({size: 100, offset: 0}, this.queryState); + 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.queryState).then(function(rows) { var docs = _.map(rows, function(row) { var _doc = new my.Document(row); _doc.backendConfig = self.backendConfig; @@ -637,10 +641,17 @@ var util = function() { var templates = { transformActions: '