2015-02-10 130 views
0

嗨,我是新來的主幹使用要求和下劃線。想要渲染模板請糾正我。不知道我要去哪裏錯了。模板不呈現下劃線錯誤

mainParentTemplate.html

<div> 
     manojbojja 
</div> 

mainWidgetnew.js

define(['jquery','underscore','backbone', 
    'text!app/templates/main/mainParentTemplate.html', 
    'masonry', 
    'imagesLoaded', 
    'Aviary', 
    'date', 
    'datetimepicker', 
    'infinitescroll', 
    'bootstrap'], 
    function($, _, Backbone, 
      MainParentViewTemplate){ 

    var RssParentView = Backbone.View.extend({ 
     template: MainParentViewTemplate, 

     initialize: function() { 
      this.render(); 
     }, 

     render: function() { 
      this.$el.html(_.template(this.template,{})); 
     } 
    }); 

    var FeedsWidget = function() { 

     this.render = function(el) { 
      this.view = new RssParentView({el: el}); 
     }; 

     return this; 
    } 

    return FeedsWidget; 

}); 

主要HTML頁面腳本

<div id="main-widget"> 
</div> 

<script> 
    window.init = function(){ 
     require(['app/mainWidgetnew'], function(FeedsWidget){ 

      var mainWidgetnew= new FeedsWidget(); 
      mainWidgetnew.render("#main-widget"); 
     }); 
    }; 
</script> 

我得到uderscore錯誤 「undefined是不是一個函數」

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

回答

0

下劃線的_.template函數將返回可用於渲染評估的函數。

你可以按照你的做渲染:

var template = _.template(this.template); 
this.$el.html(template(paramsForTemplate)); 

哪裏paramsForTemplate是可選的,可以用來傳遞參數模板。