Used underscore to template list children to utilize their simple escaped interpolation

This commit is contained in:
Luke "Jared" Bennett 2017-04-12 08:11:12 +01:00
parent a62ae94d9c
commit 04eaed8088
No known key found for this signature in database
GPG Key ID: 402ED51FB5D306C2
4 changed files with 12 additions and 10 deletions

View File

@ -2,10 +2,12 @@ const DATA_TRIGGER = 'data-dropdown-trigger';
const DATA_DROPDOWN = 'data-dropdown';
const SELECTED_CLASS = 'droplab-item-selected';
const ACTIVE_CLASS = 'droplab-item-active';
const TEMPLATE_REGEX = /\{\{(.+?)\}\}/g;
export {
DATA_TRIGGER,
DATA_DROPDOWN,
SELECTED_CLASS,
ACTIVE_CLASS,
TEMPLATE_REGEX,
};

View File

@ -93,7 +93,7 @@ Object.assign(DropDown.prototype, {
},
renderChildren: function(data) {
var html = utils.t(this.templateString, data);
var html = utils.template(this.templateString, data);
var template = document.createElement('div');
template.innerHTML = html;

View File

@ -1,19 +1,19 @@
/* eslint-disable */
import { DATA_TRIGGER, DATA_DROPDOWN } from './constants';
import { template as _template } from 'underscore';
import { DATA_TRIGGER, DATA_DROPDOWN, TEMPLATE_REGEX } from './constants';
const utils = {
toCamelCase(attr) {
return this.camelize(attr.split('-').slice(1).join(' '));
},
t(s, d) {
for (const p in d) {
if (Object.prototype.hasOwnProperty.call(d, p)) {
s = s.replace(new RegExp(`{{${p}}}`, 'g'), d[p]);
}
}
return s;
template(templateString, data) {
const template = _template(templateString, {
escape: TEMPLATE_REGEX,
});
return template(data);
},
camelize(str) {

View File

@ -63,7 +63,7 @@ require('./filtered_search_dropdown');
Object.assign({
icon: `fa-${icon}`,
hint,
tag: `<${tag}>`,
tag: `<${tag}>`,
}, type && { type }),
);
}