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 compiled templates #137

Merged
Merged
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
22 changes: 14 additions & 8 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Dataset = (function() {
function Dataset(o) {
utils.bindAll(this);

if (o.template && !o.engine) {
if (utils.isString(o.template) && !o.engine) {
$.error('no template engine specified');
}

Expand Down Expand Up @@ -285,19 +285,25 @@ var Dataset = (function() {

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

if (template) {
compiledTemplate = engine.compile(wrapper.replace('%body', template));
// precompiled template
if (utils.isFunction(template)) {
compiledTemplate = template;
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/dropdown_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ var DropdownView = (function() {
fragment = document.createDocumentFragment();

utils.each(suggestions, function(i, suggestion) {
elBuilder.innerHTML = dataset.template.render(suggestion.datum);
elBuilder.innerHTML = dataset.template(suggestion.datum);

$el = $(elBuilder.firstChild)
.css(css.suggestion)
Expand Down
16 changes: 14 additions & 2 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Dataset', function() {
});

it('should compile default template', function() {
expect(this.dataset.template.render({ value: 'boo' }))
expect(this.dataset.template({ value: 'boo' }))
.toBe('<div class="tt-suggestion"><p>boo</p></div>');
});
});
Expand All @@ -123,7 +123,9 @@ describe('Dataset', function() {
this.dataset = new Dataset({
local: fixtureStrings,
template: 't',
engine: { compile: this.spy = jasmine.createSpy().andReturn('boo') }
engine: {
compile: this.spy = jasmine.createSpy().andReturn({ render: 'boo' })
}
});
});

Expand All @@ -134,6 +136,16 @@ describe('Dataset', function() {
expect(this.dataset.template).toBe('boo');
});
});

describe('when called with a compiled template', function() {
beforeEach(function() {
this.dataset = new Dataset({ local: fixtureStrings, template: $.noop });
});

it('should use it', function() {
expect(this.dataset.template).toBe($.noop);
});
});
});

describe('#initialize', function() {
Expand Down
12 changes: 4 additions & 8 deletions test/dropdown_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,8 @@ describe('DropdownView', function() {
});

describe('#renderSuggestions', function() {
var template = {
render: function(c) {
return '<li class="tt-suggestion"><p>' + c.value + '</p></li>';
}
var template = function(c) {
return '<li class="tt-suggestion"><p>' + c.value + '</p></li>';
},
mockOldDataset = {
name: 'test',
Expand Down Expand Up @@ -572,10 +570,8 @@ describe('DropdownView', function() {
function renderTestDataset(view, open) {
var mockDataset = {
name: 'test' ,
template: {
render: function(c) {
return '<li class="tt-suggestion"><p>' + c.value + '</p></li>';
}
template: function(c) {
return '<li class="tt-suggestion"><p>' + c.value + '</p></li>';
}
},
mockSuggestions = [
Expand Down