diff --git a/src/backend.memory.js b/src/backend.memory.js index f1e3c51a..fe68a865 100644 --- a/src/backend.memory.js +++ b/src/backend.memory.js @@ -97,6 +97,11 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {}; range : range, geo_distance : geo_distance }; + var dataParsers = { + number : function (e) { return parseFloat(e, 10); }, + string : function (e) { return e.toString() }, + date : function (e) { return new Date(e).valueOf() } + }; // filter records return _.filter(results, function (record) { @@ -111,15 +116,18 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {}; // filters definitions function term(record, filter) { - return (record[filter.field] === filter.term); + var parse = dataParsers[filter.fieldType]; + var value = parse(record[filter.field]); + var term = parse(filter.term); + + return (value === term); } function range(record, filter) { - // TODO handle different data types correctly - var number = !isNaN(parseFloat(filter.start, 10)); - var start = number ? parseFloat(filter.start, 10) : filter.start; - var stop = number ? parseFloat(filter.stop, 10) : filter.stop; - var value = number ? parseFloat(record[filter.field]) : record[filter.field]; + var parse = dataParsers[filter.fieldType]; + var value = parse(record[filter.field]); + var start = parse(filter.start); + var stop = parse(filter.stop); return (value >= start && value <= stop); } diff --git a/src/model.js b/src/model.js index 9d17ba68..c3949827 100644 --- a/src/model.js +++ b/src/model.js @@ -505,6 +505,7 @@ my.Query = Backbone.Model.extend({ stop: '' }, geo_distance: { + type: 'geo_distance', distance: 10, unit: 'km', point: { diff --git a/src/widget.filtereditor.js b/src/widget.filtereditor.js index 6c7f98c9..b8885d17 100644 --- a/src/widget.filtereditor.js +++ b/src/widget.filtereditor.js @@ -121,8 +121,9 @@ my.FilterEditor = Backbone.View.extend({ var $target = $(e.target); $target.hide(); var filterType = $target.find('select.filterType').val(); - var field = $target.find('select.fields').val(); - this.model.queryState.addFilter({type: filterType, field: field}); + var field = $target.find('select.fields').val(); + var fieldType = this.model.fields.find(function (e) { return e.get('id') === field }).get('type'); + this.model.queryState.addFilter({type: filterType, field: field, fieldType: fieldType}); // trigger render explicitly as queryState change will not be triggered (as blank value for filter) this.render(); }, diff --git a/test/backend.memory.test.js b/test/backend.memory.test.js index 4c2c20ce..1df9d06f 100644 --- a/test/backend.memory.test.js +++ b/test/backend.memory.test.js @@ -3,12 +3,12 @@ module("Backend Memory - Store"); var memoryData = [ - {id: 0, x: 1, y: 2, z: 3, country: 'DE', label: 'first'} - , {id: 1, x: 2, y: 4, z: 6, country: 'UK', label: 'second'} - , {id: 2, x: 3, y: 6, z: 9, country: 'US', label: 'third'} - , {id: 3, x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'} - , {id: 4, x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'} - , {id: 5, x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'} + {id: 0, date: '2011-01-01', x: 1, y: 2, z: 3, country: 'DE', label: 'first'} + , {id: 1, date: '2011-02-03', x: 2, y: 4, z: 6, country: 'UK', label: 'second'} + , {id: 2, date: '2011-04-05', x: 3, y: 6, z: 9, country: 'US', label: 'third'} + , {id: 3, date: '2011-06-07', x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'} + , {id: 4, date: '2011-08-09', x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'} + , {id: 5, date: '2011-10-11', x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'} ]; var _wrapData = function() { @@ -18,8 +18,8 @@ var _wrapData = function() { test('basics', function () { var data = _wrapData(); - equal(data.fields.length, 6); - deepEqual(['id', 'x', 'y', 'z', 'country', 'label'], _.pluck(data.fields, 'id')); + equal(data.fields.length, 7); + deepEqual(['id', 'date', 'x', 'y', 'z', 'country', 'label'], _.pluck(data.fields, 'id')); equal(memoryData.length, data.data.length); }); @@ -82,10 +82,24 @@ test('query string', function () { test('filters', function () { var data = _wrapData(); var query = new recline.Model.Query(); - query.addFilter({type: 'term', field: 'country', term: 'UK'}); + query.addFilter({type: 'term', fieldType: 'string', field: 'country', term: 'UK'}); data.query(query.toJSON()).then(function(out) { equal(out.total, 3); - deepEqual(_.pluck(out.hits, 'country'), ['UK', 'UK', 'UK']); + deepEqual(_.pluck(out.hits, 'country'), ['UK','UK','UK']); + }); + + query = new recline.Model.Query(); + query.addFilter({type: 'range', fieldType: 'date', field: 'date', start: '2011-01-01', stop: '2011-05-01'}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 3); + deepEqual(_.pluck(out.hits, 'date'), ['2011-01-01','2011-02-03','2011-04-05']); + }); + + query = new recline.Model.Query(); + query.addFilter({type: 'range', fieldType: 'number', field: 'z', start: '0', stop: '10'}); + data.query(query.toJSON()).then(function(out) { + equal(out.total, 3); + deepEqual(_.pluck(out.hits, 'z'), [3,6,9]); }); }); @@ -156,14 +170,14 @@ function makeBackendDataset() { id: 'test-dataset', title: 'My Test Dataset', name: '1-my-test-dataset', - fields: [{id: 'x'}, {id: 'y'}, {id: 'z'}, {id: 'country'}, {id: 'label'}], + fields: [{id: 'date'}, {id: 'x'}, {id: 'y'}, {id: 'z'}, {id: 'country'}, {id: 'label'}], records: [ - {id: 0, x: 1, y: 2, z: 3, country: 'DE', label: 'first'} - , {id: 1, x: 2, y: 4, z: 6, country: 'UK', label: 'second'} - , {id: 2, x: 3, y: 6, z: 9, country: 'US', label: 'third'} - , {id: 3, x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'} - , {id: 4, x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'} - , {id: 5, x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'} + {id: 0, date: '2011-01-01', x: 1, y: 2, z: 3, country: 'DE', label: 'first'} + , {id: 1, date: '2011-02-03', x: 2, y: 4, z: 6, country: 'UK', label: 'second'} + , {id: 2, date: '2011-04-05', x: 3, y: 6, z: 9, country: 'US', label: 'third'} + , {id: 3, date: '2011-06-07', x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'} + , {id: 4, date: '2011-08-09', x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'} + , {id: 5, date: '2011-10-11', x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'} ] }); dataset.fetch(); @@ -227,11 +241,25 @@ test('query string', function () { test('filters', function () { var dataset = makeBackendDataset(); - dataset.queryState.addFilter({type: 'term', field: 'country', term: 'UK'}); + dataset.queryState.addFilter({type: 'term', fieldType: 'string', field: 'country', term: 'UK'}); dataset.query().then(function() { equal(dataset.records.length, 3); deepEqual(dataset.records.pluck('country'), ['UK', 'UK', 'UK']); }); + + dataset = makeBackendDataset(); + dataset.queryState.addFilter({type: 'range', fieldType: 'date', field: 'date', start: '2011-01-01', stop: '2011-05-01'}); + dataset.query().then(function() { + equal(dataset.records.length, 3); + deepEqual(dataset.records.pluck('date'), ['2011-01-01','2011-02-03','2011-04-05']); + }); + + dataset = makeBackendDataset(); + dataset.queryState.addFilter({type: 'range', fieldType: 'number', field: 'z', start: '0', stop: '10'}); + dataset.query().then(function() { + equal(dataset.records.length, 3); + deepEqual(dataset.records.pluck('z'), [3,6,9]); + }); }); test('facet', function () {