1

这是我的代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Matt's Template</title>

        <!-- Stylesheets -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css" type="text/css" />
        <link rel="stylesheet" href="../stylesheets/general.css" type="text/css" />

        <style type="text/css">
            .dragndrop {
                position:relative;
                margin:30px auto;
                border:4px dashed #000;
                border-radius: 25px;
                padding:50px;
                text-align: center;
            }
            table{
                width:100%;
            }
            tr{
                width:100%;
            }
            td, th {
                padding:10px;
                border:1px solid #000;
                width:50%;
                text-align: center;
            }
        </style>
    </head>
    <body>
            <section class="container">

                <!--<form action="/file-upload" method="post" enctype="multipart/form-data">
                  <input type="file" name="file" />
                  <input type="submit" />
                </form>-->      
                <form action="/file-upload" class="dropzone dragndrop" id="my-awesome-dropzone"></form>     

                <section id="skills">

                </section>


                <script type="text/template" id="skillsTemplate">
                    <section>
                        <table>
                            <tr>
                                <th>Skill</th>
                                <th>Value</th>
                            </tr>
                            <% _.each(items, function(item){ %>
                                    <tr>
                                        <td><%= item.get('name') %></td>
                                        <td><%= item.get('value') %></td>
                                    </tr>
                            <% }); %>
                            <tr>
                                <td><button id="newSkill">New</button></td>
                            </tr>
                        </table>
                    </section>
                </script>
            </section>

        <!-- Javascript Libraries -->
        <script type="text/javascript" src="https://raw.github.com/enyo/dropzone/master/downloads/dropzone.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js" ></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone.js"></script>

        <script type="text/javascript">
             SkillsView = Backbone.View.extend({
                render : function(){
                    var template = _.template($('#skillsTemplate').html(), [{ name:"Linux", value:"Test"}]);

                    this.$el.html(template);
                }
            });

             var skillsview = new SkillsView({el : $('#skills') });

             skillsview.render();


        </script>
        <!-- My Javscript -->
    </body>
</html>

唯一重要的部分是下划线模板不起作用。就是说行上的“_”:_.each(items, function(item)未定义。为什么会这样?我尝试将下划线脚本包含移动到页面顶部,但没有帮助。

4

1 回答 1

2

我无法重现“_未定义”问题,但我确实发现了您可能遇到的另一个问题:您将项目引用为变量items,但您从未告诉过_.template您希望数据在items. 使用对象字面量作为数据:

_.template($('#skillsTemplate').html(), {
    items: [{ name:"Linux", value:"Test" }]
})

(此外,item.get('name')当数据是普通对象而不是下划线模型时,您正在使用,但我认为这只是您简化问题代码后原始代码的残余。)

于 2013-05-14T05:40:10.700 回答