制造灰尘的侵入性最小的方法是什么.js能够提前渲染打字.js模板
What's the least intrusive way to make dust.js able to render typeahead.js templates?
typeahead.js 使我们能够使用我们选择的引擎为我们的自动完成建议呈现模板,只要引擎实现了这个 API:
// engine has a compile function that returns a compiled template
var compiledTemplate = ENGINE.compile(template);
// compiled template has a render function that returns the rendered template
// render function expects the context to be first argument passed to it
var html = compiledTemplate.render(context);
现在Dust.js对此事的看法略有不同:
var compiled = dust.compile("Hello {name}!", "intro");
dust.loadSource(compiled);
由于我已经集成了灰尘.js我也想使用它来渲染 typeahead 的建议。我可能可以包装 dust 的引擎对象并提供所需的 API,但我想知道是否有一种侵入性更小和/或更优雅的方式来做到这一点,例如通过将所需的功能动态附加到 dust 对象本身?
编辑添加:混合@user2103008和@Simon所拥有的东西,这是我在 typeahead-0.9.3 中使用的内容:
function typeaheadFakeCompile(dustTemplateName) {
return function(data) {
var html;
dust.render(dustTemplateName, data, function(err, out) { html = out; })
return html;
}
}
var compiled = dust.compile('Hello {name}!', 'myTemplate');
dust.loadSource(compiled);
$('selector').typeahead({
template: typeaheadFakeCompile('myTemplate')
)};
传递给 typeahead 插件的 template
参数可以是编译的模板或字符串。如果它是一个字符串,则预类型插件将尝试编译它。不要用灰尘做这件事。相反,像往常一样编译 dust 模板,但将模板参数传递为如下所示:
var compiled = dust.compile('Hello {name}!', 'myTemplate');
dust.loadSource(compiled);
$('selector').typeahead({
template: fakeCompile('myTemplate')
)};
function fakeCompile (dustTemplateName) {
return {
render: function (data) {
var html;
dust.render(dustTemplateName, data, function (err,out) { html = out });
return html;
}
}
}
Typeahead 应按原样使用"已编译"模板,而无需尝试其他编译。
编辑得益于@user2103008,修复了灰尘渲染回调函数签名。
相关文章:
- 在客户端上编译和渲染复杂的Dust.js模板
- Undercore.js模板无法使用json
- 未能创建对handelbars.js模板的ajax请求
- 在 Windows 中预编译车把.js模板
- 从 JavaScript 文件中挖空 JS 模板
- 挖空JS模板使UI在编辑时“闪现”
- 流星JS模板占位符
- 流星js模板助手导致页面首次加载时出错
- PHP还是JS模板引擎?(对于同一html的多个块)
- 在单个文件中需要js和多个已编译的Handlebar.js模板
- Angular.js模板中的条件表达式
- jQuery.click()赢得't从<a>在splash.js模板中
- 如何将预编译的hogan.js模板封装到AMD模块中
- Ember.js模板布局
- meteor.js模板助手作为变量与键+值
- 递归Handlebars.js模板.如何确定深度
- 如何访问Angular JS模板指令中设置的值,如“ng class?”
- backbone.js模板中的echo-php变量
- 使用handlers.js模板以数组中的最后一项为条件
- 类似Jinja的JS模板语言