Javascript 主干视图未捕获参考错误
如果你能帮助我理解为什么我会得到一份未完成的工作,我将不胜感激 参考错误(参见下面的代码)。 本质上,在视图初始化时,我正在获取一个模型并在 我正在将“this.model”——实例模型——传递给 模板。在所有其他视图中,即使实例模型 未定义未捕获的引用错误不会被抛出。有人知道吗 为什么把它扔在这里Javascript 主干视图未捕获参考错误,javascript,backbone.js,Javascript,Backbone.js,如果你能帮助我理解为什么我会得到一份未完成的工作,我将不胜感激 参考错误(参见下面的代码)。 本质上,在视图初始化时,我正在获取一个模型并在 我正在将“this.model”——实例模型——传递给 模板。在所有其他视图中,即使实例模型 未定义未捕获的引用错误不会被抛出。有人知道吗 为什么把它扔在这里 Views.Projects.EditView = Backbone.View.extend({ tagName: 'div', id: 'edit-project-conten
Views.Projects.EditView = Backbone.View.extend({
tagName: 'div',
id: 'edit-project-content',
template: JST['projects/edit'],
initialize: function(){
this.model = new Models.Project({id: this.options.projectId});
this.model.bind('change', this.render, this);
this.model.fetch({
error: function(model, response) { alert('Error...Please try again.'); }
});
},
render: function() {
$(this.el).html(this.template({project: this.model})); // Error references this line.
return this;
}
});
模板:
<% if (typeof project != 'undefined') { %>
<div id="edit-details">
<form id="edit-project-form">
<ul>
<li>
<p class='form-title'>Edit Project: "<%= project.get('title') %>"</p>
</li>
<li>
<label for='project-title'>Project Title:</label>
<input id='project-title' type='text' value="<%= project.get('title') %>" />
</li>
<li>
<label for='due-date'>Due Date:</label>
<input id='due-date' type='text'></input>
</li>
<li>
<label for='project-description'>Description:</label>
<textarea id='project-description'><%= project.get('description') %></textarea>
</li>
<li>
<input id='submit-project-edits' type='submit' value='Edit' />
</li>
</ul>
</form>
</div>
<% } %>
-
编辑项目:“
-
项目名称:
-
到期日:
-
说明:
-
谢谢。试试这个:
render: function() {
model = this.model;
$(this.el).html(this.template({project: model})); // Error references this line.
return this;
}
你可以改变
<% if (typeof project != 'undefined') { %>
尝试访问null或未定义的属性将在JS中引发异常。(我有时有点怀念ActionScript2:)我复制了您的代码,当我在控制台中调试它时,我得到了以下错误 未捕获类型错误:对象没有方法“get” 我在控制台中看到了“project”模型,它正在将模型绑定到模板。如果你能复制它,这可能是你这边的一条线索 我在我的申请中也遇到了同样的问题,这就是我所做的,并且成功了。我必须在视图中指定模型的类型,它可以工作。试试看是否有帮助。希望能有帮助
Views.Projects.EditView = Backbone.View.extend({
tagName: 'div',
**model : your model**
id: 'edit-project-content',
template: JST['projects/edit'],
initialize: function(){
this.model = new Models.Project({id: this.options.projectId});
this.model.bind('change', this.render, this);
this.model.fetch({
error: function(model, response) { alert('Error...Please try again.'); }
});
},
render: function() {
$(this.el).html(this.template({project: this.model})); // Error references this line.
return this;
}
});
这是使用浏览器的调试器查看模板函数中实际发生的情况的绝佳时机。你的模板中到底有什么?@biziclop:我已将模板添加到我的OP中。我已使用浏览器的调试器调试了模板函数。模板试图获取项目的属性,但未能获取时,会抛出异常。@muistooshort:获取的模型已正确格式化为JSON;无论如何,我试过你的建议,但不幸的是,它不起作用。在
this.model.fetch()
完成之前,你能调用render
吗?@muistooshort:我把你的例子放在一边,让它更接近我的代码;看一看--render
在视图初始化时被调用(并且view initialize函数触发模型进行获取),因此如果在获取完成之前调用render,我不会感到惊讶。但是这不应该是一个问题,因为模板检查未定义的项目/模型;不幸的是,它没有解决这个错误。
<% if( project ) { %>
typeof undefined === 'undefined'
typeof null === 'object'
Views.Projects.EditView = Backbone.View.extend({
tagName: 'div',
**model : your model**
id: 'edit-project-content',
template: JST['projects/edit'],
initialize: function(){
this.model = new Models.Project({id: this.options.projectId});
this.model.bind('change', this.render, this);
this.model.fetch({
error: function(model, response) { alert('Error...Please try again.'); }
});
},
render: function() {
$(this.el).html(this.template({project: this.model})); // Error references this line.
return this;
}
});