[FIX] make SearchQuery#add(Array) work correctly (fixing #reset at the same time)
authorXavier Morel <xmo@openerp.com>
Tue, 24 Apr 2012 12:38:24 +0000 (14:38 +0200)
committerXavier Morel <xmo@openerp.com>
Tue, 24 Apr 2012 12:38:24 +0000 (14:38 +0200)
bzr revid: xmo@openerp.com-20120424123824-x3nsecztz9gsiar4

addons/web/static/src/js/search.js
addons/web/static/test/search.js

index f37cf1a..4c18623 100644 (file)
@@ -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");
-        }
-        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;
+        if (!(values instanceof Array)) {
+            values = [values];
         }
-        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 = {});
index c8d23ca..7403afb 100644 (file)
@@ -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');