首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从外部html文件加载handlebars.js模板时未显示任何内容

从外部html文件加载handlebars.js模板时未显示任何内容
EN

Stack Overflow用户
提问于 2012-10-26 02:42:23
回答 1查看 20.2K关注 0票数 2

这个问题有点超出了我的JS技能,所以我可能会像个笨蛋一样解释它。

下面是我的JavaScript,用于从服务器获取json,并尝试将其推入模板:

代码语言:javascript
复制
    //Server Interface Start
//Access the web api for The User:
var lucidServer = (function () {
    //global error handler
    $(document).ajaxError(function (event, xhr) {
        alert(xhr.status + " : " + xhr.statusText);
    });
    //client Calls
    var getClients = function (id) {
        return $.ajax(clientUrl + "/list/" + id)
    };
    var getClient = function (id) {
        return $.ajax(clientUrl + "/details/" + id)
    };

    //push them out to the world!
    return {
        getClients: getClients,
        getClient: getClient,
    };
}());
//Server Interface End

(function () {
    //list of clients
    var refreshClients = function () {
        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();
    });
}());

GetClients运行良好,并返回我期望的Json数据。当我检查templateInput时,它显示了我所期望的内容。

以下是外部模板:

代码语言:javascript
复制
<script id="clientList" type="text/html">

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

我最终的目标是将我的所有模板保存在一个可以通过脚本id调用的html文件中,或者让每个模板都是我可以调用的自己的文件。当我运行此页面时,没有显示任何内容,也没有错误或其他任何东西。下面是整个索引页:

代码语言:javascript
复制
    <!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="../../Content/normalize.css">
    <link rel="stylesheet" href="../../Content/main.css">
    <script src="../../scripts/libs/modernizr-2.6.2.min.js"></script>
</head>
<body>
    <!--[if lt IE 7]>
            <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
        <![endif]-->
    <section id="Application">
        <div id="clientListOutput">
        </div>
        <div id="clientCreateOutput">
        </div>
    </section>
    <footer>
    </footer>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../scripts/libs/jquery-1.8.2.min.js"><\/script>')</script>
    <script src="../../scripts/plugins.js"></script>
    <script src="../../scripts/main.js"></script>
    <script type="text/javascript" src="../../Scripts/libs/handlebars.js"></script>
    <script>
        var freelancerUrl = '@Url.RouteUrl("ApiControllerAndIntegerId", new { httproute="", controller = "Freelancer"})';
        var clientUrl = '@Url.RouteUrl("ApiControllerAction", new { httproute="", controller = "Client"})';
        var projectUrl = '@Url.RouteUrl("ApiControllerAction", new { httproute="", controller = "Project"})';
        var storyUrl = '@Url.RouteUrl("ApiControllerAndIntegerId", new { httproute="", controller = "Story"})';
    </script>
    <script type="text/javascript" src="../../Scripts/serverInterface.js"></script>
</body>
</html>

编辑

我稍微改变了我的模板的外观。通过在Chrome工具中调试js,它告诉我在showallclient中的ajax成功行中没有定义'data‘。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-26 02:54:24

需要使用getClient返回的数据调用template。在您的示例中,showAllClients的参数被成功回调中的参数所隐藏,因为您使用了相同的名称(data)。将showAllClients中的参数名称更改为其他名称,并将其传递给模板函数。

代码语言:javascript
复制
(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();
        });
    }());

编辑:

在模板中,您需要引用每个块中的this

代码语言:javascript
复制
<div id="clients">
    {{#each client}}
                <span data-id="{{this.id}}">
                    <div>
                        <p>{{this.iD}}</p>
                        <p>{{this.name}}</p>
                     ...
                    </div>
        {{/each}}
</div>

在您的示例中,您使用了嵌套迭代器,我不确定它是否受支持。有关不同的方法,请阅读How do I use nested iterators with Mustache.js or Handlebars.js?Ember.js / Handlebars nested each iterations

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13075132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档