Loading handlebars.js template from external html file shows nothing

↘锁芯ラ 提交于 2019-12-01 00:51:29
ebaxt

You need to call template with the data returned from getClient. In you example the argument to showAllClients is shadowed by the argument in the success callback because you use the same name (data). Change the argument name in showAllClients to something else, and pass it to the template function.

(function () {

        //list of clients
        var refreshClients = function () {
           $.when(lucidServer.getClients(1)).done(showAllClients);
        };
        var showAllClients = function (templateInput) {
            var source;
            var template;
            var path = 'Templates/indexTemplates.html'
            $.ajax({
                url: path,
                cache: true,
                success: function (data) {
                    source = data;
                    template = Handlebars.compile(source);
                    $('#clientListOutput').html(template(templateInput));
                }
            });
        };

        $(function () {

            refreshClients();
        });
    }());

EDIT:

In the template you need to refer to this inside the each block:

<div id="clients">
    {{#each client}}
                <span data-id="{{this.id}}">
                    <div>
                        <p>{{this.iD}}</p>
                        <p>{{this.name}}</p>
                     ...
                    </div>
        {{/each}}
</div>

In your example you use nested iterators, which I'm not sure is supported. Please read How do I use nested iterators with Mustache.js or Handlebars.js? or Ember.js / Handlebars nested each iterations for a different approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!