Search code examples
javascriptbackbone.jsmarionette

Marionette - constructor vs initialize


I noticed that Marionette views define a constructor method, instead of initialize. What's the purpose of it - why not just use Backbone's initialize, which is still being called during instantiation?


Solution

  • It's common to implement initialize in your own View types.

    If Marionette Views used initialize, you would need to remember to call the parent method to get the default behaviour. Most of your Views would look like this:

    var MyView = Marionette.ItemView.extend({
      initialize: function(options) {
        Marionette.ItemView.prototype.initialize.call(this);
        this.x = options.x;
      }
    });
    

    This isn't necessary because Marionette uses constructor rather than initialize.