I'd appreciate help in understanding why I'm getting an uncaught reference error (see code below). Essentially, upon view initialization I'm fetching a model and within the render method I'm passing "this.model" -- the instance model -- to the template. In all other views, even when the instance model is undefined an uncaught reference error is NOT thrown. Does anyone know why it is thrown here?
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;
}
});
Template:
<% 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>
<% } %>
Thanks.
renderbefore thethis.model.fetch()completes? - mu is too shortrenderis called as soon as the view is initialized (and the view initialize function triggers the model to fetch), so I wouldn't be surprised if render is called before fetch is completed. But that shouldn't be an issue, since the template checks for an undefined project/model. - Dan