gokujou
gokujou

Reputation: 1491

Having trouble extending a Backbone view

I am probably missing something easy or doing something wrong, but I am trying this and can't get it to fire the function...

var Home = Backbone.View.extend({
    indexAction: function() {
        console.log('index');
    },
    render: function() {
        console.log('render');
    }
});
Home.indexAction();

All I get is this error:

Uncaught TypeError: Object function (){return i.apply(this,arguments)} has no method 'indexAction'

Upvotes: 1

Views: 41

Answers (1)

Austin Greco
Austin Greco

Reputation: 33544

You created the view type but did not create an instance. You need to instantiate a view of type Home now:

var h = new Home();
h.indexAction();

Also, it might be better to rename Home as HomeView, so you know it's a view which can be instantiated.

var HomeView = Backbone.View.extend({
    indexAction: function() {
        console.log('index');
    },
    render: function() {
        console.log('render');
    }
});

var home = new HomeView();

example on backbone docs

Upvotes: 3

Related Questions