add new methods for facets: removeFacet, clearFacets, and refreshFacets
This commit is contained in:
23
src/model.js
23
src/model.js
@@ -570,7 +570,30 @@ my.Query = Backbone.Model.extend({
|
|||||||
};
|
};
|
||||||
this.set({facets: facets}, {silent: true});
|
this.set({facets: facets}, {silent: true});
|
||||||
this.trigger('facet:add', this);
|
this.trigger('facet:add', this);
|
||||||
|
},
|
||||||
|
removeFacet: function(fieldId) {
|
||||||
|
var facets = this.get('facets');
|
||||||
|
// Assume id and fieldId should be the same (TODO: this need not be true if we want to add two different type of facets on same field)
|
||||||
|
if (!_.contains(_.keys(facets), fieldId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete facets[fieldId];
|
||||||
|
this.set({facets: facets}, {silent: true});
|
||||||
|
this.trigger('facet:remove', this);
|
||||||
|
},
|
||||||
|
clearFacets: function() {
|
||||||
|
var facets = this.get('facets');
|
||||||
|
_.each(_.keys(facets), function(fieldId) {
|
||||||
|
delete facets[fieldId];
|
||||||
|
});
|
||||||
|
this.trigger('facet:remove', this);
|
||||||
|
},
|
||||||
|
// trigger a facet add; use this to trigger a single event after adding
|
||||||
|
// multiple facets
|
||||||
|
refreshFacets: function() {
|
||||||
|
this.trigger('facet:add', this);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -353,6 +353,24 @@ test('Query.addFacet', function () {
|
|||||||
deepEqual({terms: {field: 'xyz', "size": 25}}, query.get('facets')['xyz']);
|
deepEqual({terms: {field: 'xyz', "size": 25}}, query.get('facets')['xyz']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Query.removeFacet', function () {
|
||||||
|
var query = new recline.Model.Query();
|
||||||
|
query.addFacet('xyz');
|
||||||
|
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||||
|
query.removeFacet('xyz');
|
||||||
|
equal(undefined, query.get('facets')['xyz']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Query.clearFacets', function () {
|
||||||
|
var query = new recline.Model.Query();
|
||||||
|
query.addFacet('abc');
|
||||||
|
query.addFacet('xyz');
|
||||||
|
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||||
|
deepEqual({terms: {field: 'abc'}}, query.get('facets')['abc']);
|
||||||
|
query.clearFacets();
|
||||||
|
deepEqual({}, query.get('facets'));
|
||||||
|
});
|
||||||
|
|
||||||
test('Query.addFilter', function () {
|
test('Query.addFilter', function () {
|
||||||
var query = new recline.Model.Query();
|
var query = new recline.Model.Query();
|
||||||
query.addFilter({type: 'term', field: 'xyz'});
|
query.addFilter({type: 'term', field: 'xyz'});
|
||||||
|
|||||||
Reference in New Issue
Block a user