user3350508
user3350508

Reputation: 123

Extending Backbone functions while keeping base functionality

I'm attempting to add a bit of processing functionality to Backbone.View in the initialize function that I want to be carried over to all my Backbone Views. The problem is, I'm using Marionette so I can't do something like this:

var BaseView = Backbone.View.extend({})

because Marionette extends Backbone.View itself. Here's what I would like to do:

// Add processoring logic to an extended version of Backbone. 
Backbone.View.extend({
    initialize: function(options){
       if(options.hasOwnProperty("vents") {
         // process vents
       }

       // native code. Calling the library's actual original function to maintain original functionality.
       Backbone.View.initialize(this, aurguments);          

    }
})

var CollectionView = new Marionette.CollectionView({
     vents: {VentCallName: function(){}}
     // When initialize is called, it'll see the vents and deal with them automatically.
});

I'm just not sure how to add the functionality to Backbone.View while maintaining whatever function logic is already in there.

EDIT

How do I actually get the initial extended functionality into Backbone.View.initialize without making a new extended instance and basing all my views off that? I can't get Marionette to use that extended view, so the extra processing has to go into Backbone.View's initialize function.

If I do this, it loops back on itself:

Backbone.View.prototype.initialize = function(){
    console.log("moooo");
            // custom logic then run Backbone.View.initialize native code. 
    Backbone.View.prototype.initialize.apply(this, arguments);
}

Upvotes: 1

Views: 537

Answers (1)

Kevin Ennis
Kevin Ennis

Reputation: 14466

Backbone.View.prototype.initialize.apply(this, arguments);

Edit

Okay, well that's a slightly different question.

var oldInitialize = Backbone.View.prototype.initialize;
Backbone.View.prototype.initialize = function(){
  console.log("moooo");
  // custom logic then run Backbone.View.initialize native code. 
  oldInitialize.apply(this, arguments);
}

Upvotes: 2

Related Questions