如何识别未定义的内容

时间:2013-01-07 00:50:41

标签: backbone.js

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

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

这最后一行给了我这个错误

未捕获的TypeError:无法读取未定义的属性“el”

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

   outerHTML: "<div><h1>Enki</h1>↵↵Entries go here↵</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>

有人能告诉我什么是'未定义'以及如何解决它?

1 个答案:

答案 0 :(得分:2)

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

尝试更改渲染功能,以便将引用返回给this对象。

render: function(){
  $(this.el).html(this.template( {entries: "Entries go here"} ))
  return this // <- important
}