JavaScript模板(下划线)

时间:2013-06-12 20:47:42

标签: javascript jquery underscore.js

下划线让我难过!在我的代码中,一切都与$ .when之后获取数据有关。 console.log(帖子);工作,但当我尝试将其传递到模板和参考

<h1><%=posts.id %></h1> 

我在第

行收到“Uncaught ReferenceError:posts not defined”
$("#target").html(_.template(template,posts));

这是整个页面

<!DOCTYPE html>
<html>
<head>  
<script src="js/api.js"></script>   
<link href="css/styles.css" media="" rel="stylesheet" type="text/css" /> 
</head>
<body>

<div id="target"></div>

<!-- BEGIN: Underscore Template Definition. -->

<script type="text/template" id="template">     
<h1><%=posts.id %></h1>
</script>

<!-- END: Underscore Template Definition. -->

<!-- Include and run scripts. -->

 <script src="js/jquery-1.9.1.min.js"></script>
 <script src="js/underscore.js"></script>

 <script type="text/javascript">            

    $.when(results).done(function(posts){       

    var template = $("#template").html();
    console.log(posts);
    $("#target").html(_.template(template,posts)
    );
    }); 

</script> 

</body>

[Object]
 0: Object
 created_at: "2013-04"
 id: "444556663333"
 num_comments: 1
 num_likes: 0
 text: "<p>dfgg</p>"
 title: "title1"
 updated_at: "2013-04"
 user: Object
   first_name: "bob"
   id: "43633"
   last_name: "ddd"

**** Upadated * ** < EM> * ** 感谢大家,我的模板正常运行。 _.each循环遍历一个对象数组,并从API中填充html和数据块。现在,我需要做的是弹出一个具有特定帖子内容的模态。我正在努力解决我的.click事件。所有不同的模态填充正确的数据(当隐藏,引导模态)但我不知道如何在我点击相应的div时引用它们。我总是得到第一篇文章的帖子内容。

$(".datachunk").click(function (){ 
$("#myModal").modal();    
});

.datachunk指的是您单击的当前div.datachunk。这是我的模板:

  <!-- BEGIN: Underscore Template Definition. -->
<script type="text/template" id="template">     
 <% _.each(posts,function(post){ %>   
    <div class = "datachunk borderBottom">  
    <div class="openModall"><i class="icon-plus-sign-alt"></i> </div>
    <h2><%= post.title %></h2>
 <div class="postInfo">
      <p><%= post.user.first_name %><%= post.user.last_name %></p><p>
   <% var date=moment(post.created_at).format("M/DD/YYYY");%>
       <%= date %></p>              
     </div>
</div> <!--datachunk-->
  <% }) %>

  <!--BEGIN Modal-->
      <% _.each(posts,function(post){ %>   
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-             labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <div class="datachunk">
     <div class= "postInfo">                
          <h2><%= post.title %></h2>
    <p><%= post.user.first_name %><%= post.user.last_name %></p>
      </div>
    </div>
  </div> <!--end modal header-->
  <div class="modal-body">
    <p><%=post.text %></p>
  </div> 
</div>
<!--END Modal-->
<% }) %>                
</script>
<!-- END: Underscore Template Definition. -->

3 个答案:

答案 0 :(得分:5)

替换

$("#target").html(_.template(template,posts));

$("#target").html(_.template(template,{posts:posts}));

希望这应该有用。

另见:How to use underscore.js as a template engine?

编辑:基于来自console.log的更新信息,因为它是一个数组,因为@Shanimal指出你需要引用该数组中的第一项或循环它(更好的方法)。

请参阅@ Shanimal的帖子进行循环播放。你仍然需要按照我的指示去做。

答案 1 :(得分:5)

就像你最初发布的那样......

$("#target").html(_.template(template,{posts:posts}));

然后

<script type="text/template" id="template">     
    <% _.each(posts,function(v,i,l){ %> 
    <h1><%= v.id %></h1>
    <% }) %>
</script>

答案 2 :(得分:0)

var posts =

    [

       {
            id:1,
            post:"post 1"
        },

        {
            id:2,
            post:"post 2"
        },
        {
            id:3,
            post:"post 3"
        },
        {
            id:4,
            post:"post 4"
        },
        {
            id:5,
            post:"post 5"
        }
    ];



  <div id="target_5"></div>


  <script type="text/template" id="template_5">
   <% _.each(posts,function(post,index,arr){ %>
    <h1><%= post.id %></h1>
   <% }); %>
 </script>
相关问题