ragulka
ragulka

Reputation: 4342

Chaplin Backbone framework - views - how to use initialize and render methods?

I need to execute some code both on initialize and render methods, but as I understand, I canot just modfy them directly when using Chaplin - when I define my own initialize method, my routes stop working.

I also tried afterInitialize() but it seems its not meant to be overrided: https://github.com/chaplinjs/chaplin/issues/168#issuecomment-8015915

Upvotes: 1

Views: 699

Answers (1)

Concordus Applications
Concordus Applications

Reputation: 987

[...] but as I understand, I canot just modfy them directly when using Chaplin

You can modify them directly as long you appropriately delegate to the extended prototype.

As you haven't tagged your question javascript or coffeescript, the following are two solutions for each one. First up is javascript. Notice how we must explicitly invoke the extended function.

var View = Chaplin.View.extend({
  initialize: function(options) {
    // Add code here ..

    // The below invokes the initialize function of the
    // base Chaplin.View class in the context of 
    // this class
    Chaplin.View.prototype.initialize.call(this, arguments);

    // .. or here
  }
});

Next is coffeescript, which makes this kind of thing significantly easier.

class View extends Chaplin.View
  initialize: ->
    // Add code here ..

    // The below auto-magically invokes the 
    // initialize method of the base class
    super

    // .. or here

Upvotes: 1

Related Questions