Skip to content

Commit

Permalink
Allow displayKey to be a function. Closes #633.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Feb 9, 2014
1 parent 51b55f5 commit a11ca76
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/typeahead/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var Dataset = (function() {
this.name = o.name || _.getUniqueId();

this.source = o.source;
this.valueKey = o.displayKey || 'value';
this.displayFn = getDisplayFn(o.display || o.displayKey);

this.templates = getTemplates(o.templates, this.valueKey);
this.templates = getTemplates(o.templates, this.displayFn);

this.$el = $(html.dataset.replace('%CLASS%', this.name));
}
Expand Down Expand Up @@ -107,7 +107,7 @@ var Dataset = (function() {
outerHtml = html.suggestion.replace('%BODY%', innerHtml);
$el = $(outerHtml)
.data(datasetKey, that.name)
.data(valueKey, suggestion[that.valueKey])
.data(valueKey, that.displayFn(suggestion))
.data(datumKey, suggestion);

$el.children().each(function() { $(this).css(css.suggestionChild); });
Expand Down Expand Up @@ -166,7 +166,15 @@ var Dataset = (function() {
// helper functions
// ----------------

function getTemplates(templates, valueKey) {
function getDisplayFn(display) {
display = display || 'value';

return _.isFunction(display) ? display : displayFn;

function displayFn(obj) { return obj[display]; }
}

function getTemplates(templates, displayFn) {
return {
empty: templates.empty && _.templatify(templates.empty),
header: templates.header && _.templatify(templates.header),
Expand All @@ -175,7 +183,7 @@ var Dataset = (function() {
};

function suggestionTemplate(context) {
return '<p>' + context[valueKey] + '</p>';
return '<p>' + displayFn(context) + '</p>';
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/dataset_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ describe('Dataset', function() {
expect(this.dataset.getRoot()).toContainText('three');
});

it('should allow custom display functions', function() {
this.dataset = new Dataset({
name: 'test',
display: function(o) { return o.display; },
source: this.source = jasmine.createSpy('source')
});

this.source.andCallFake(fakeGetForDisplayFn);
this.dataset.update('woah');

expect(this.dataset.getRoot()).toContainText('4');
expect(this.dataset.getRoot()).toContainText('5');
expect(this.dataset.getRoot()).toContainText('6');
});

it('should render empty when no suggestions are available', function() {
this.dataset = new Dataset({
source: this.source,
Expand Down Expand Up @@ -167,6 +182,10 @@ describe('Dataset', function() {
]);
}

function fakeGetForDisplayFn(query, cb) {
cb([{ display: '4' }, { display: '5' }, { display: '6' } ]);
}

function fakeGetWithSyncEmptyResults(query, cb) {
cb();
}
Expand Down

0 comments on commit a11ca76

Please sign in to comment.