2013-05-14 92 views
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)是未定义的。这是为什么发生?我试着将包含下划线的脚本移动到页面的顶部,这没有帮助。

回答

2

我无法重现“_没有定义”的问题,但我没有找到,你可能会遇到了另一个问题:你引用的项目作为变量items,但你从来没有告诉过_.template你想要的数据将在items。使用对象文本的数据:

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

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

+0

哦,我想我明白了为什么它这样做......所以我用ExpressJS与和的NodeJS表达是使用EJS渲染引擎。所以我认为当我使用<% %>时,它认为我正在访问来自嵌入式JavaScript的数据。你认为这是吗?它在服务器端而不是客户端出现错误 – 2013-05-14 05:45:02

+1

@MattHintzke:是的,这听起来似乎合理。看看[这个问题](http://stackoverflow.com/q/9021587)。 – icktoofay 2013-05-14 06:00:39

+0

你是老板。这绝对是为什么 – 2013-05-14 06:17:52