0

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

Here is my view:

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

and here is my error:

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

Where am I wrong?

nikoshr
  • 32,926
  • 33
  • 91
  • 105
Prajwol Onta
  • 1,448
  • 5
  • 21
  • 48

1 Answers1

0

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

Try

var myModel  = // define your model
var todoView = new TodoView({
    model: myModel
});
nikoshr
  • 32,926
  • 33
  • 91
  • 105
  • phew.. thank you so much @nikoshr i didnt' know we have to load model that way... i havn't seen that in any tutorials yet.. THANK YOU. – Prajwol Onta Jan 18 '13 at 10:20