2013-10-24 55 views
0

我有我的觀點:預編譯車把模板無法呈現

var ProductsView = Backbone.View.extend({ 

    initialize: function(){ 
     var that = this; 
     this.collection.fetch({ 
      success: function() 
       { 
        console.log("fetcheo"); 
        that.render(); 
       } 
     }); 

     this.listenTo(this.collection, "reset", this.render); 
    }, 

    render: function(){ 

     var cats = []; 
     this.collection.each(function(model) 
          { 
           cats.push(model.get('familia')); 
          }); 
     this.cats = _.uniq(cats, false); 
      console.log(this.cats) // It returns ["VINOS", "CERVEZA", "BOTANA"] 
     this.$el.html(Handlebars.templates.products(this.cats)); 
     return this; 
    } 
}); 

這是預編譯的車把模板:

<h1>Y LOS MODELOS SON</h1> 
<ul> 
{{#each cats}} 
<li> 
{{this}} 
</li> 
{{/each}} 
</ul> 

不過,這並不呈現this.cats陣列; 這不是一個收集問題()我已經修復了一個早期的問題。 感謝您的幫助......

回答

1

你只需要包裝的對象在「貓」屬性:

this.cats = { cats: _.uniq(cats, false) } 

注意,當您使用{{#each cats}},渲染器將尋找一個財產命名「貓」。你的變量名稱是「貓」,但渲染器根本看不到。

Fiddle demo

+0

感謝名單了很多!!!! –