My model attribute in my view is being classified as a function

So I am trying to link my view to my model and I am following the instructions perfectly, however when it comes to the model part I am just stumped.

Whenever I try to define the model via instantiation, the model is being classified as a function when I console.log() it out.

But let me show you.

var ListModel = Backbone.Model.extend({
    defaults: {
        name: "Miles",
        last: "Coleman"
    }
});

var ListView = Backbone.View.extend({

    initialize: function(opts){
        this.template = opts.template;
        this.render();
    },

    render: function() {
        var data = this.model.toJSON();
        console.log(this.model);
        // outputs: function (){a.apply(this,arguments)} 
    }
});

var view = new ListView({
    model: ListModel,
    el: 'div',
    template: _.template('#todo-template')
});

Is there some silly detail that I'm missing here? Thanks!

Upvotes: 1

Views: 37

Answers (1)

nikoshr
nikoshr

Reputation: 33364

You're passing the class itself, ListModel, to the view, but a view expects an instance of the class, new ListModel() for example. Try

var view = new ListView({
    model: new ListModel(),
    el: 'div',
    template: _.template('#todo-template')
});

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript for more information on classes and instances in JS.

Upvotes: 3

Related Questions