2014-04-12 94 views
1

我試圖嵌入與骨幹子視圖胸/車把,我的代碼如下:骨幹/胸部呈現子視圖失敗

查看

child: new BunnyRabbitsListView(), 
template: template, 
render: function() { 
    $(this.el).html(this.template({ child: this.child })); 
} 

模板

<div>{{view child}}</div> 

BunnyRabbitsListView模板

<h2>Hello World</h2> 

我在我的瀏覽器控制檯得到一個錯誤:

Uncaught TypeError: Cannot read property '_helperName' of undefined 

和堆棧跟蹤:

Uncaught TypeError: Cannot read property '_helperName' of undefined thorax.js:948 
getParent thorax.js:948 
(anonymous function) thorax.js:983 
(anonymous function) VM218:10 
(anonymous function) handlebars.js:2212 
(anonymous function) handlebars.js:2172 
jQuery.extend.access jquery.js:849 
jQuery.fn.extend.html jquery.js:6015 
collection.fetch.success index.js:20 
options.(anonymous function) thorax.js:2931 
(anonymous function) thorax.js:2894 
_.each._.forEach underscore.js:78 
(anonymous function) thorax.js:2892 
options.success thorax.js:1405 
options.success backbone.js:856 
fire jquery.js:1017 
self.fireWith jquery.js:1127 
done jquery.js:8021 
callback jquery.js:855 

,這是index.js:20 FWIW

$(this.el).html(this.template({ child: this.child })); 
+0

是不是'template'的功能?像:'this。$ el.html(this.template())'...另外,在你的代碼中如何使用'child'?對我來說,我認爲在視圖定義中實例化一個視圖是奇怪的......不應該是'child:function(){return new ServiceProvidersListView();}'所以你不會有每個視圖相同的孩子? –

+0

它是有意義的,我已經改變了代碼,因爲反映了這一點(現在更新主要帖子,模板是一個需要傳遞給它的函數,但對於這個例子,this.child是一個視圖isn這是一個問題,它仍然會中斷。)但這不是我看來的問題的來源...... :( – Zen

回答

2

我已經創建了一個工作示例在jsfiddle上嵌入父視圖的子視圖。 雖然我沒有使用渲染方法。讓我知道它是否有幫助。

這裏是工作代碼:

var BunnyRabbitsListView = Thorax.View.extend({ 
    template: Handlebars.compile("<p>Hello World</p>") 
}); 

var ParentView = Thorax.View.extend({ 
    child: new BunnyRabbitsListView(), 
    template: Handlebars.compile("{{view child}}") 
}); 

var parentView = new ParentView(); 
parentView.appendTo('body');