handlebars.js模板是在服务器端生成的,并作为响应的一部分返回。
"<div>{{name}}<small style='color:#999;'><br><code>{{vafm}}</code></small><br><code>{{email}}</code></div>"
。
var t = "";
$('#UniqueId').select2({
ajax: {
url: '/search/endpoint',
dataType: 'json',
processResults: function (data) {
t = data.template;
return {
results: data.results
};
},
},
templateResult: Handlebars.compile(t),
escapeMarkup: function (m) {
return m;
}
});
很遗憾,select2上的渲染部分不包含data.results
返回的值
我已将问题定位到此行
templateResult: Handlebars.compile(t),
因为尝试过类似的东西
<script>
const template = Handlebars.compile(@Html.Raw(Model.Template));
</script>
和
templateResult: template,
按预期工作。
在上一个示例中,我从模型传递了模板,
但不是我需要从ajax响应传递它并获得相同的输出。
存在多个问题: