recipeswithbackbone / recipeswithbackbone.github.com

Marketing site for the best damn backbone.js book evar
23 stars 1 forks source link

change our inheritance style #23

Open ngauthier opened 12 years ago

ngauthier commented 12 years ago

We're using _.extend's ability to accept additional mixin classes for exension:

Modules.X = {
  foo: function() { }
}

Views.Y = Backbone.View.extend(_.extend({
  bar: function() { }
}, Modules.X));

This is simple and is a great way to do mixins in a ruby-module-style. However, we're using this in a few recipes where "real inheritance" would be more appropriate than a mixin:

Views.X = Backbone.View.extend({{
  foo: function() { }  
});

// Then the view just extends the other view
Views.Y = Views.X.extend({
  bar: function() { }
});

The difference here is mixin vs inheritance. I think for the most part we're using mixins right in our recipes, but there may be a few cases for inheritance.

This is taking advantage of a special behavior of Backbone's extend (which is different from _.extend) and is self-propagating: http://documentcloud.github.com/backbone/#Model-extend