2

我的主干应用程序出现未定义的错误。我无法弄清楚什么是未定义的。在路由器的索引功能里,我有这个

  var view = new Enki.Views.EntriesIndex();
  console.log(view);
  $('#container').html(view.render().el);

最后一行给了我这个错误

未捕获的类型错误:无法读取未定义的属性“el”

表明console.log(view)正在view = new Enki.Views.EntriesIndex();实例化。部分日志包含

   outerHTML: "<div><h1>Enki</h1>↵↵Entries go here↵&lt;/div>"
   outerText: "Enki↵↵Entries go here↵"

该日志输出来自视图的渲染功能

render: function(){
  $(this.el).html(this.template({
  entries: "Entries go here"
  }));
 }

这是模板。。

  <h1>Enki</h1>

  <%= entries %>

...它没有被插入到“容器”div中,而是在视图中被读取,如上面的日志输出所示。没有任何内容被插入到主页上的容器 div 中。

<div id="container">...</div>

谁能告诉我什么是“未定义”以及如何解决它?

4

1 回答 1

2

一个合理的假设是您的render函数不返回任何内容,因此view.render()未定义。

尝试更改您的渲染函数,使其返回对this对象的引用。

render: function(){
  $(this.el).html(this.template( {entries: "Entries go here"} ))
  return this // <- important
}
于 2013-01-07T00:54:52.147 回答