Skip to content

Commit

Permalink
Merge pull request #53 from edx/dan-f/update-filter-getters
Browse files Browse the repository at this point in the history
Update getActivefilterfields, add getFilterFieldValue
  • Loading branch information
dan-f committed Apr 5, 2016
2 parents 7042fde + 89900ce commit 9759688
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
28 changes: 22 additions & 6 deletions src/js/pagination/paging-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,30 @@
},

/**
* Gets an array of currently active (applied) filters.
* @returns {Array} An array of filter field names which are
* currently applied to the collection.
* Gets an object of currently active (applied) filters.
* @returns {Object} An object mapping the names of
* currently active filter fields to their values.
*/
getActiveFilterFields: function () {
return _.chain(this.filterableFields).pick(function (fieldData, fieldName) {
return !_.isNull(fieldData.value) && !_.isUndefined(fieldData.value);
}).keys().value();
return _.chain(this.filterableFields)
.pick(function (fieldData, fieldName) {
return !_.isNull(fieldData.value) && !_.isUndefined(fieldData.value);
})
.mapObject(function (data) {
return data.value;
})
.value();
},

/**
* Gets the value of the given filter field.
*
* @returns {String} the current value of the requested
* filter field. null or undefined means that the
* filter field is not active.
*/
getFilterFieldValue: function (filterFieldName) {
return this.getActiveFilterFields()[filterFieldName];
},

/**
Expand Down
14 changes: 13 additions & 1 deletion src/js/pagination/specs/paging-collection-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,19 @@ define(['jquery',
collection.registerFilterableField('test_field_3', 'Test Field 3');
collection.setFilterField('test_field_1', 'test_value_1');
collection.setFilterField('test_field_3', 'test_value_3');
expect(collection.getActiveFilterFields()).toEqual(['test_field_1', 'test_field_3']);
expect(collection.getActiveFilterFields())
.toEqual({test_field_1: 'test_value_1', test_field_3: 'test_value_3'});
});

it('can get the value of a particular filter field', function () {
collection.registerFilterableField('test_field_1', 'Test Field 1');
collection.registerFilterableField('test_field_2', 'Test Field 2');
collection.setFilterField('test_field_1', 'test_value_1');
expect(collection.getFilterFieldValue('test_field_1')).toEqual('test_value_1');
collection.unsetFilterField('test_field_1');
expect(collection.getFilterFieldValue('test_field_1')).toBe(undefined);
expect(collection.getFilterFieldValue('test_field_2')).toBe(undefined);
expect(collection.getFilterFieldValue('no_such_field')).toBe(undefined);
});

it('can set the sort direction', AjaxHelpers.requests(
Expand Down

0 comments on commit 9759688

Please sign in to comment.