Backbone / thorax渲染子视图失败

时间:2014-04-12 05:25:37

标签: backbone.js javascript

我试图使用Backbone Thorax / Handlebars嵌入子视图,我的代码如下:

查看

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 }));

1 个答案:

答案 0 :(得分: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');