Calling Super Method in Javascript Backbone Class

I have a base class that extends a Backbone.View. I want to be able to call the super's 'initialize' after overriding the 'initialize' on a sub class. How can I accomplish calling the super of an extended class in javascript in the most robust and clearest way?

I have seen this (Super in Backbone), is there a clearer way to accomplish this without having to know who the super class is before hand?

App.Views.BaseView = Backbone.View.extend({
    initialize: function(templateContext){
        this.super_called = true;
    }
});

For all of my sub views, I want to take advantage of the already written initialize function.

App.Views.ChildViewWorks = App.Views.extend({});
var newView = new App.Views.ChildViewWorks();
alert(newView.super_called); // print true

App.Views.ChildViewDoesNotWork = App.Views.extend({
    initialize: function(templateContext){
        this.super_called = false;
        //what line of code can I add here to call the super initialize()?
    }   
});
var newViewWrong = new App.Views.ChildViewDoesNotWork();
alert(newViewWrong.super_called); //now equal to false because I have not called the super.

Upvotes: 1

Views: 1319

Answers (1)

Creynders
Creynders

Reputation: 4583

App.Views.ChildViewDoesNotWork = App.Views.BaseView.extend({
    initialize: function(templateContext){
        this.super_called = false;
        App.Views.BaseView.prototype.initialize.call(this);
    }   
});

Upvotes: 2

Related Questions