From 595eec05a6e241518bcc4bb82aa5fe0dc000a425 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 24 Apr 2012 14:38:24 +0200 Subject: [PATCH] [FIX] make SearchQuery#add(Array) work correctly (fixing #reset at the same time) bzr revid: xmo@openerp.com-20120424123824-x3nsecztz9gsiar4 --- addons/web/static/src/js/search.js | 30 +++++++++++++++++------------- addons/web/static/test/search.js | 20 ++++++++++---------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index f37cf1ae60b..4c1862365ca 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -51,21 +51,25 @@ my.SearchQuery = Backbone.Collection.extend({ this.remove(facet); }, this); }, - add: function (value, options) { + add: function (values, options) { options || (options = {}); - if (value instanceof Array) { - throw new Error("Can't add multiple facets to a query at once"); + if (!(values instanceof Array)) { + values = [values]; } - var model = this._prepareModel(value, options); - var previous = this.detect(function (facet) { - return facet.get('category') === model.get('category') - && facet.get('field') === model.get('field'); - }); - if (previous) { - previous.values.add(model.get('values')); - return this; - } - return Backbone.Collection.prototype.add.call(this, model, options); + + _(values).each(function (value) { + var model = this._prepareModel(value, options); + var previous = this.detect(function (facet) { + return facet.get('category') === model.get('category') + && facet.get('field') === model.get('field'); + }); + if (previous) { + previous.values.add(model.get('values')); + return; + } + Backbone.Collection.prototype.add.call(this, model, options); + }, this); + return this; }, toggle: function (value, options) { options || (options = {}); diff --git a/addons/web/static/test/search.js b/addons/web/static/test/search.js index c8d23caf49c..7403afb5d0d 100644 --- a/addons/web/static/test/search.js +++ b/addons/web/static/test/search.js @@ -24,15 +24,16 @@ $(document).ready(function () { equal(facet.get('field'), field); deepEqual(facet.get('values'), [{label: 'Value', value: 3}]); }); - test('Adding an array of facet is not valid', function () { + test('Adding two facets', function () { var query = new instance.web.search.SearchQuery; + query.add([ + { category: 'Foo', field: {}, values: [{label: 'Value', value: 3}] }, + { category: 'Bar', field: {}, values: [{label: 'Value 2', value: 4}] } + ]); - raises(function () { - query.add([ - { category: 'Foo', field: {}, values: [{label: 'Value', value: 3}] }, - { category: 'Bar', field: {}, values: [{label: 'Value 2', value: 4}] } - ]); - }); + equal(query.length, 2); + equal(query.at(0).values.length, 1); + equal(query.at(1).values.length, 1); }); test('If a facet already exists, add values to it', function () { var query = new instance.web.search.SearchQuery; @@ -107,7 +108,7 @@ $(document).ready(function () { equal(query.length, 0, 'Should have removed the facet'); }); - test('Intermediate emptyness should not remove the facet', function () { + test('Intermediate emptiness should not remove the facet', function () { var field = {}; var query = new instance.web.search.SearchQuery; query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]}); @@ -124,14 +125,13 @@ $(document).ready(function () { {label: 'V2', value: 1} ]); }); - // TODO: if the last FacetValue of a facet is removed, the facet should be removed from the query test('Reseting with multiple facets should still work to load defaults', function () { var query = new instance.web.search.SearchQuery; var field = {}; query.reset([ {category: 'A', field: field, values: [{label: 'V1', value: 0}]}, - {category: 'B', field: field, values: [{label: 'V2', value: 1}]}]); + {category: 'A', field: field, values: [{label: 'V2', value: 1}]}]); equal(query.length, 1, 'Should have created a single facet'); equal(query.at(0).values.length, 2, 'the facet should have merged two values');