0

我正在使用下划线将 JSON 数据转换为模板:

$(document).ready(function(){
    var template = $("#furniture-template").html();
        $furnitureList = _.template(template, {things: things});
});

它正常加载并且在进行正常页面刷新时工作正常。但是当通过 ajax 加载页面时(使用 history.js 和 ajaxify.js)我得到两个错误:

Error: SyntaxError: syntax error
Source Code: <% _.each(things,function(thing,key,list){ %> 

Error: TypeError: n is undefined
Source File: http://192.168.0.20:8888/metropolis/library/themes/metropolis/assets/js/plugins/underscore.js

下面是模板脚本。

有没有人有这样的下划线错误的经验?我应该如何进行调试,或者如果有人能指出我在这里做错了什么,那就太好了。

谢谢!

            <script type="text/html" id='furniture-template'>
                <div class="accordion collapse">
                    <div class="accordion-group">
                        <% _.each(things,function(thing,key,list){ %>

                        <div class="accordion-heading">
                            <a class="accordion-toggle ic-minus block collapsed" data-toggle="collapse" href="#things-<%= thing.slug %>">
                                <%= thing.title %>
                            </a>
                        </div> <!-- header -->

                        <div id="things-<%= thing.slug %>" class="accordion-body collapse">
                            <div class="accordion-inner">
                                <% for(var item in thing.items) { %>
                                <div class="item">
                                    <% if( thing.items[item].images == true ) { %>
                                        <a data-target="<%= thing.items[item].slug %>-gal" class="img-link ic-cam fl" title="View an example"></a>
                                    <% } %>

                                    <a 
                                        class="item-add ic-plus" 
                                        data-title="<%= thing.items[item].title %>" 
                                        data-slug="<%= thing.items[item].slug %>"
                                        data-img="<%= thing.items[item].images %>"
                                        data-shorthand="<%= thing.items[item].shorthand %>"
                                        data-price="<%= thing.items[item].price %>"
                                    >
                                        <%= thing.items[item].title %>
                                    </a>
                                </div>
                                <% } %>
                            </div> <!-- inner -->
                        </div> <!-- accordion-body -->  


                    <% }); %>
                    </div>
                </div>
            </script>
4

1 回答 1

0

ajaxify 脚本在 ajax 成功时异步加载脚本。问题是它正在创建一个没有or属性的<script>节点。typeid

这是来自 ajaxify.js 的修改后的脚本摘录

// Add the scripts
$scripts.each(function(){
    var $script = $(this), 
        scriptText = $script.text(),
        scriptType = $script.attr('type'),
        scriptId = $script.attr('id');

    scriptNode = document.createElement('script');

    if(scriptType) {
        $(scriptNode).attr('type', scriptType);
    }

    if(scriptId) {
        $(scriptNode).attr('id', scriptId);
    }

    scriptNode.appendChild(document.createTextNode(scriptText));
    contentNode.appendChild(scriptNode);
});
于 2013-06-14T02:46:27.883 回答