在控制台中显示的主干模型误差 [英] Backbone model error displayed in console

查看:107
本文介绍了在控制台中显示的主干模型误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习开发由阿迪·奥斯马尼的Backbone.js的应用,我卡住了。

I'm learning Developing Backbone.js Applications by Addy Osmani and I am stuck.

下面是我的看法:

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

我在哪里错了?

推荐答案

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

You are not passing any model to your view, hence the this.model is undefined

尝试

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

这篇关于在控制台中显示的主干模型误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆