我在主干应用程序中遇到了未定义的错误。我无法弄清楚什么是未定义的。在路由器的索引功能中,我有这个
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↵</div>"
outerText: "Enki↵↵Entries go here↵"
该日志输出来自 View 的渲染函数
render: function(){
$(this.el).html(this.template({
entries: "Entries go here"
}));
}
这是模板..
<h1>Enki</h1>
<%= entries %>
...这没有被插入到“容器”div 中,而是在 View 中读取,如上面的日志输出所示。主页上的容器 div 中没有插入任何内容。
<div id="container">...</div>
谁能告诉我什么是“未定义”以及如何解决它?
最佳答案
一个合理的假设是您的 render
函数不会返回任何内容,因此 view.render()
未定义。
尝试更改渲染函数,以便它返回对 this
对象的引用。
render: function(){
$(this.el).html(this.template( {entries: "Entries go here"} ))
return this // <- important
}
关于backbone.js - 如何识别未定义的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14188458/