控制台中显示的主干模型错误

时间:2013-01-18 10:09:57

标签: javascript backbone.js

我正在学习Addy Osmani的Developing Backbone.js Applications,而且我被困住了。

以下是我的观点:

var TodoView = Backbone.View.extend({
    tagName: 'li',
    className: 'todo_list',
    todoTpl: _.template($('#item-template').html()),
    events:{
        'dblclick label': 'edit',
        'keypress .edit':'updateOnEnter',
        'blur .edit':'closed'
    },
    initialize:function(){
        _.bindAll(this, 'edit','render','updateOnEnter','closed');
        this.render();
    },
    render: function(){
        this.$el.html(this.todoTpl(this.model.toJSON()));
        this.input = this.$('.edit');
        return this;
    },
    edit: function(){},
    updateOnEnter: function(){},
    closed: function(e){}
});

var todoView = new TodoView();
console.log(todoView.el);

这是我的错误:

 TypeError: this.model is undefined
 this.$el.html(this.todoTpl(this.model.toJSON()));

我哪里错了?

1 个答案:

答案 0 :(得分:0)

您没有将任何模型传递给您的视图,因此 this.model未定义

尝试

var myModel  = // define your model
var todoView = new TodoView({
    model: myModel
});