Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'local' bloodhound option as a function that returns an array #673

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bloodhound/options_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ var oParser = (function() {
};

function getLocal(o) {
return o.local || null;
var local = o.local || null;

if (_.isFunction(local)) {
local = local.call(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use call here. Instead, you can just do:

local = local();

Also a shorter way of writing this would be:

function getLocal(o) {
  var local = o.local || null;

  return _.isFunction(local) ? local() : local;
}

Some people don't like the conditional operator, but I use it all the time. Really is just a preference call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that using .call(null) can prevent potential misuse of 'this' in the function that's being passed in (doesn't seem like it could happen now, but could guard against future changes)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad idea, but that would only make a difference in strict mode. In non-strict mode, both local() and local.call(null) will invoke local with the context set to the global object i.e. window.

This actually brings up a good point, should typeahead.js should run in strict mode? I'm thinking yes. I'm going to create a separate issue to track that change/discussion.

}

return local;
}

function getPrefetch(o) {
Expand Down
58 changes: 44 additions & 14 deletions test/bloodhound_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,56 @@ describe('Bloodhound', function() {
});

describe('local', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple
describe('when local is an array', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple
});

this.bloodhound.initialize();
});

this.bloodhound.initialize();
it('should hydrate the bloodhound', function() {
var spy = jasmine.createSpy();

this.bloodhound.get('big', spy);

expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
});

it('should hydrate the bloodhound', function() {
var spy = jasmine.createSpy();
describe('when local is a function that returns an array', function() {
beforeEach(function() {
var localFn = function() {
return fixtures.data.simple;
};

this.bloodhound.get('big', spy);
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: localFn
});

expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
this.bloodhound.initialize();
});

it('should hydrate the bloodhound', function() {
var spy = jasmine.createSpy();

this.bloodhound.get('big', spy);

expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
});
});

Expand Down