Backbone View Uncaught Reference Error

时间:2012-02-13 20:13:57

标签: javascript backbone.js

我很感激帮助理解为什么我没有受到教育 参考错误(见下面的代码)。 基本上,在视图初始化时,我正在获取模型并在其中 我正在传递“this.model” - 实例模型 - 的render方法 模板。在所有其他视图中,即使实例模型是 未定义未引发未捕获的引用错误。有人知道吗 为什么会被抛到这里?

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>
<% } %>

感谢。

3 个答案:

答案 0 :(得分:0)

试试这个:

render: function() {
    model = this.model;
    $(this.el).html(this.template({project: model})); // Error references this line.
    return this;
}

答案 1 :(得分:0)

你可以改变

<% if (typeof project != 'undefined') { %>

<% if( project != null ) { %>

<% if( project ) { %>

请参阅https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof

typeof undefined === 'undefined'
typeof null === 'object'

尝试访问null或undefined的属性将在JS中引发异常。 (我有时会想念ActionScript 2)。

答案 2 :(得分:0)

我复制了你的代码,当我在控制台中调试它时,我收到以下错误。

未捕获的TypeError:对象#没有方法'get'

我在控制台中看到了“项目”模型,它将模型绑定到模板。如果您可以复制它,这可能是您的主角。

我在我的应用程序中遇到了同样的问题,这就是我所做的并且有效。我必须在视图中指定模型的类型并且它有效。试试看,看看是否有帮助。希望能帮助到你。

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;
    }
});