2013-04-21 110 views
0

我試圖在我的頁面上呈現搜索框。我試圖保持在一個單獨的觀點。沒有這個boxview的頁面正常工作,但只要我初始化我的BoxView我得到與下劃線相關的錯誤。視圖不在Backbone.js中渲染

Uncaught TypeError: Object [object Object] has no method 'replace' underscore-min.js:29 

這是我的看法

/* Views */ 

var BoxView = Backbone.View.extend({ 
    el: '.head', 
    initialize: function() { 
     this.render(); 
    }, 
    render : function(){ 
     var that = this; 
     var template = _.template($('#search-box').html()); 
     that.$el.html(template); 
    } 
}); 

    var boxview = new BoxView(); 

模板

<script type="text/template" id="search-box"> 
<form class="navbar-search pull-right" id="search"> 
<input class="search-query" name="searchText" type="text" id="searchText" placeholder="Search books"/> 
<button type="submit" class="btn">Submit</button> 
</form> 
</script> 

編輯: 刪除了錯字錯誤

+0

可能重複的[Underscore.js模板問題 - 無法調用方法'替換'null](http://stackoverflow.com/questions/11001419/underscore-js-template-issue-cannot-call-method-replace -of-null) – 2013-04-21 16:20:27

+0

你在_ _template($('#search-box')之前調用'_.template(null)'某處,檢查$('#search-box')中的內容。 html的())'。 – 2013-04-21 16:21:25

回答

0

在我看來,這只是一個錯字:

var template = _.template($('#search-box')).html(); 

應該是:

var template = _.template($('#search-box').html()); 

編輯:
我通常認爲這是種在寫這個問題作出錯字,但你的錯誤提示的問題來自於一個下劃線通話(你只有一個),並且它試圖在對象上使用replace_.template肯定使用replace,你給它一個對象($('#search-box'))。所以這是有道理的。

+0

我從問題中刪除了錯字。同樣的錯誤仍然 – Abhishek 2013-04-21 10:49:55

+0

@Ahishek你確定你沒有在你的代碼中的錯字嗎? http://jsfiddle.net/5bYHF/工作正常。 – Loamhoof 2013-04-21 11:54:59

+0

是的,代碼中沒有錯別字。實際上有兩種觀點。隨着他們兩個到位我得到錯誤。一旦我刪除這個,它工作正常 – Abhishek 2013-04-21 14:19:25