0

我正在学习Addy Osmani 的开发 Backbone.js 应用程序,但我被卡住了。

这是我的看法:

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

我哪里错了?

4

1 回答 1

0

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

尝试

var myModel  = // define your model
var todoView = new TodoView({
    model: myModel
});
于 2013-01-18T10:16:19.217 回答