Giter VIP home page Giter VIP logo

Comments (5)

jferris avatar jferris commented on June 26, 2024

We originally wrote this before Backbone's extend changed. I have no reason to believe this can't be updated now. Thanks for catching that.

from backbone-support.

ngauthier avatar ngauthier commented on June 26, 2024

yep NP. In fact ... recipeswithbackbone/recipeswithbackbone.github.com#23 :-D

from backbone-support.

jferris avatar jferris commented on June 26, 2024

I tried using a fix similar to the change @domchristie used, but it means that you need to manually apply the CompositeView initialize method if you have your own initializer, which breaks compatibility and is a step backwards. Has anybody found a way to use Backbone's extend without manually calling initializers?

from backbone-support.

domchristie avatar domchristie commented on June 26, 2024

I think the inheritance pattern used in CompositeView is acceptable, given the need to add (pre-)initialize code. However, there is slight improvement that could be made.

I came across a similar issue when trying to extend the Backbone.Model class with some code that ran before the initialize method was called. Using the 'old' inheritance pattern (i.e. similar to that currently being used in CompositeView) caused all kinds of issues with Backbone collections, because Backbone.Model's prototype property was not included in the prototype chain of the new model class. Or to demonstrate with some code:

// Backbone.MyNewModel inherits from Backbone.Model using 'old' pattern

new Backbone.MyNewModel() instanceof Backbone.MyNewModel // returns true
new Backbone.MyNewModel() instanceof Backbone.Model // returns false

Relating this to CompositeView and you get:

new Support.CompositeView() instanceof Backbone.View // returns false

To fix this, I used some code from the Backbone source to 'do' inheritance. It's very similar to currently exists, but just adds the parent's prototype to the child's prototype chain:

;(function() {
  var ctor = function(){},
      View = Backbone.View,
      CompositeView;

  CompositeView = Support.CompositeView = function(options) {
    View.apply(this, [options]);
    // pre-initialize methods go here
  };

  ctor.prototype = View.prototype;
  CompositeView.prototype = new ctor();

  _.extend(CompositeView.prototype, View.prototype, {
    // instance methods go here
  });

  CompositeView.extend = View.extend;
})();

And with this pattern:

new Support.CompositeView() instanceof Support.CompositeView; // returns true
new Support.CompositeView() instanceof Backbone.View; // returns true

Now this may not be as important as with a Backbone.Model but thought it was worth sharing anyway.

from backbone-support.

jferris avatar jferris commented on June 26, 2024

@domchristie interesting stuff. I haven't run into a situation where that's mattered before, but I'd take a pull request to correct the constructor chain.

from backbone-support.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.