使用在服务器端生成的handlebars.js模板

问题描述 投票:0回答:1

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返回的值enter image description here

我已将问题定位到此行

templateResult: Handlebars.compile(t),

因为尝试过类似的东西

<script>
    const template = Handlebars.compile(@Html.Raw(Model.Template));
</script>

templateResult: template,

按预期工作。

在上一个示例中,我从模型传递了模板,

但不是我需要从ajax响应传递它并获得相同的输出。

javascript asp.net-mvc handlebars.js asp.net-ajax jquery-select2
1个回答
1
投票

存在多个问题:

© www.soinside.com 2019 - 2024. All rights reserved.