Extending Backbone.js constructors, the easy way

One thing that really is really annonying with backbone 1.1 is the fact that the options are not passed has this.options anymore in the Backbone.View.

Well simply enough we can add this (or anything else) in the view constructor.

// Compatibility override - Backbone 1.1 got rid of the 'options' binding
// automatically to views in the constructor - we need to keep that.
Backbone.View = (function(View) {
   return View.extend({
        constructor: function(options) {
            this.options = options;
            View.apply(this, arguments);
        }
    });
})(Backbone.View);

You can also augment the constructor with anything you need. This is useful, for example, if all views need to call some functions when initialized, or if you need to set some data on all views.

You could also use this pattern to augment any Backbone constructor, the possibilities are endless!

avatar
Cedric Dugas is a front-end veteran with more than 9 years under the belt, between his open source projects & entrepreneurial ambitions he is a product architect at CakeMail. Why don't you follow him on twitter.

13 Comments on "Extending Backbone.js constructors, the easy way"

  1. avatar _kud says:

    Thanks buddy. Save my day. 😉

  2. I am regular reader, how are you everybody? This article posted at this web site is in fact pleasant.

    Look at my blog – news bloopers

  3. Normally I do not learn article on blogs, but I wish to say that this write-up
    very pressured me to try and do so! Your writing style has
    been amazed me. Thank you, quite great article.

  4. Aw, this was a very good post. Taking the time and actual effort to create a very good article… but what can I say… I
    put things off a lot and don’t manage to get anything done.

  5. Right now it seems like Drupal is the top blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  6. avatar anecdote says:

    Hello my loved one! I want to say that this article is amazing, nice written and include approximately all important infos.
    I would like to peer more posts like this .

  7. Hi, yup this paragraph is in fact nice and I have learned lot of things from it
    on the topic of blogging. thanks.

  8. Britain’s second biggest city was for many years regarded as little more than a centre
    for industry and was once dubbed ‘The workshop of the world’.

  9. I like ԝҺat ʏoս guys ɑге usuɑlly սp too. Thiss type оf
    cllever worek аnd reporting! Ò eep uρ the wonderful Õ¡orks guys I’vе yߋu
    guys tο my personal blogroll.

  10. So as to start the USB microscope, simply double click
    on the USB camera. Some of the gear you are able to keep the next time
    around, but eventually that can break and you
    will have to buy new gear with the points you earn from surviving.

    Everything is based on quality, at times you might get duplicate products but they are of no
    use because of poor quality. Quality is something that needs to be taken care when you out on prowl for such things.

  11. I’m not sure exactly why but this site is loading
    very slow for me. Is anyone else having this issue or is it a
    issue on my end? I’ll check back later and see if the problem still exists.

  12. avatar Evie says:

    Thank you for the auspicious writeup. It in truth was once a enjoyment account it.
    Glance complicated to more delivered agreeable from you! However, how could we keep up a correspondence?

  13. Great information. Lucky me I came across your site by chance (stumbleupon).
    I’ve bookmarked it for later!

Got something to say? Go for it!