2013-12-17 58 views
0
<script id="entry-template" type="text/x-handlebars-template"> 
     <h2>This is the template</h2> 
     {{ count }} items 
    </script> 
    <script type="text/javascript"> 
     var MyNamespace = {}; 

     $(document).ready(function() { 

      MyNamespace.Recipe = Backbone.View.extend({ 
       tagName: "li", 
       render: function() { 
        return this.$el.text("Chicken Chilly"); 
       } 
      }) 


      MyNamespace.MyTagView = Backbone.View.extend({ 

       initialize: function() { 
        this.render(); 
       }, 
       render: function() { 
        var template = Handlebars.compile($("#entry-template").html()); 
        this.$el.html(template); 
        return this; 
       }, 
       count: 4 
      }); 
      var View = new MyNamespace.MyTagView(); 
      $("#content").html(View.el); 
     }); 


    </script> 

我得到的輸出爲0的項目,而不是4項查看沒有得到所需要的輸出

回答

1

在這一行:

template: template('entry-template'), 

你生成你的編譯模板和'entry-template'上下文的HTML對象(它不是一個id,它是你的模板的值對象)。生成HTML後,您將其分配到的MyNamespace.MyTagView

template財產後來,在render方法,則需要調用這個模板屬性(即HTML),因爲它是一個功能:

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

但不是函數,而是包含生成的html的屬性。

你應該這樣分配的模板:

template: template, 
+0

是的好友時,該固定它。謝謝! –