2013-03-15 101 views
1

我有像數組以下有問題通過JSON對象循環

var data = [ 
      { 
       title: 'This is title', 
       desc: 'This is desc', 
       date: '07:12' 
      }, 
      { 
       title: 'This is title2', 
       desc: 'This is desc2', 
       date: '04:12' 
      }, 
      { 
       title: 'This is title3', 
       desc: 'This is desc3', 
       date: '09:12' 
      } 
     ]; 

現在我想遍歷這個數據使用underscorejs模板來顯示。我正在嘗試下面哪個不工作。

<% _.each(function() { %> 
     <li> 
      <span class="time"><%= date %></span> 
      <p><%= title %></p> 
      <p><%= desc %></p> 
     </li> 
    <% }); %> 

上面的代碼並不顯示任何東西,它也不會在控制檯中顯示任何錯誤。我怎樣才能循環這個數組數據來顯示所有的數據?

UPDATE

下面是一些更多的代碼。我從骨幹視圖

var Message = Backbone.View.extend({ 
    className: 'tops', 

    render: function() { 
     console.log(this.model.toJSON()); //<-- see output for this below 
     this.$el.html(_.template(MessageTemplate, this.model.toJSON())); 
     return this; 
    } 
}); 

的console.log()輸出

Object {title: "This is title", desc: "This is desc", date: "07:12"} message.js:6 
Object {title: "This is title2", desc: "This is desc2", date: "04:12"} message.js:6 
Object {title: "This is title3", desc: "This is desc3", date: "09:12"} 

我傳遞上述目的,以我的模板,然後通過循環它表明通過這個數據。

+0

這是無效的JSON。這些對象是否應該放在數組中? – 2013-03-15 17:07:32

+0

是的,它是數組。 – 2619 2013-03-15 17:07:46

+0

請參閱'_.each'的文檔http://underscorejs.org/#each – travis 2013-03-15 17:08:23

回答

1

你需要指定你循環通過對象:

<% _.each(data,function (elem) { %> 
    <li> 
     <span class="time"><%= elem.date %></span> 
     <p><%= elem.title %></p> 
     <p><%= elem.desc %></p> 
    </li> 
<% }); %> 

underscore documentation

+0

這顯示了這個錯誤'未捕獲的類型錯誤:無法調用未定義的方法'調用' – 2619 2013-03-15 17:09:27

+0

這讓我懷疑其他代碼有其他錯誤。我試過在我的盒子上寫一個簡單的測試,它工作。你介意創建一個簡化的說明你的問題的jsfiddle嗎? – 2013-03-15 17:10:18

+0

其實這不是一個json對象。這是一個數組。 – 2619 2013-03-15 17:11:24

0

underscorejs.org

When you evaluate a template function, pass in a data object that has properties corresponding to the template's free variables. If you're writing a one-off, you can pass the data object as the second parameter to template in order to render immediately instead of returning a template function.

你在模板中使用下劃線代碼工作它,如果你是把它通常寫出來的一樣。 _.each需要至少兩個參數。首先是您要迭代的項目列表,其次是要對每個項目執行的操作。

_.each([1, 2, 3], alert); 

您需要對模板執行相同操作。

<% _.each(dataSet, function (item) { %> 
    <li> 
     <span class="time"><%= item.date %></span> 
     <p><%= item.title %></p> 
     <p><%= item.desc %></p> 
    </li> 
<% }); %> 

var rendered_html = _.template(template, {dataSet: data}) 

通知傳遞到_.template第二個目的是如何包含的dataSet的關鍵。我們傳入_.template的密鑰可以在我們的模板代碼中用於引用與它們相關的值。

這裏你可以看到一個例子:http://jsbin.com/upugaz/1/edit

+0

顯示了這個錯誤'未捕獲的ReferenceError:數據未定義' – 2619 2013-03-15 17:47:13

+0

這可能是因爲數據未定義...檢查jsbin鏈接。 – travis 2013-03-15 17:59:31

+0

這不是一個全功能的主幹示例,但它應該可以幫助你。 http://jsbin.com/upugaz/3/edit – travis 2013-03-15 18:01:24

0

你有路過時到模板給一個名字數組:

this.$el.html(_.template(MessageTemplate, { 'items': this.model.toJSON() })); 

現在,你可以參考你想要的數組循環播放您的模板:

<% _.each(items, function (item) { %> 
    <li> 
     <span class="time"><%= item.date %></span> 
     <p><%= item.title %></p> 
     <p><%= item.desc %></p> 
    </li> 
<% }); %>