Skip to content

Commit

Permalink
Fix bug introduced by twitter#137.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Mar 31, 2013
1 parent da624c5 commit 2069bd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,28 +295,30 @@ var Dataset = (function() {

function compileTemplate(template, engine, valueKey) {
var wrapper = '<div class="tt-suggestion">%body</div>',
renderFn,
wrappedTemplate,
compiledTemplate;

// precompiled template
if (utils.isFunction(template)) {
compiledTemplate = template;
renderFn = template;
}

// string template that needs to be compiled
else if (utils.isString(template)) {
wrappedTemplate = wrapper.replace('%body', template);
compiledTemplate = engine.compile(wrappedTemplate).render;
compiledTemplate = engine.compile(wrappedTemplate);
renderFn = utils.bind(compiledTemplate.render, compiledTemplate);
}

// if no template is provided, render suggestion
// as its value wrapped in a p tag
else {
compiledTemplate = function(context) {
renderFn = function(context) {
return wrapper.replace('%body', '<p>' + context[valueKey] + '</p>');
};
}

return compiledTemplate;
return renderFn;
}
})();
10 changes: 6 additions & 4 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,22 @@ describe('Dataset', function() {

describe('when called with a template and engine', function() {
beforeEach(function() {
this.spy = jasmine.createSpy().andReturn({
render: function() { return 'boo!'; }
});

this.dataset = new Dataset({
local: fixtureStrings,
template: 't',
engine: {
compile: this.spy = jasmine.createSpy().andReturn({ render: 'boo' })
}
engine: { compile: this.spy }
});
});

it('should compile the template', function() {
expect(this.spy)
.toHaveBeenCalledWith('<div class="tt-suggestion">t</div>');

expect(this.dataset.template).toBe('boo');
expect(this.dataset.template()).toBe('boo!');
});
});

Expand Down

0 comments on commit 2069bd9

Please sign in to comment.