Comments (5)
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.
yep NP. In fact ... recipeswithbackbone/recipeswithbackbone.github.com#23 :-D
from backbone-support.
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.
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.
@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)
- Different behaviour Chrome and Firefox when using prop('selectedIndex',-1) HOT 3
- [enhancement] Add missing bower.json.
- Explain if this library is still supported HOT 6
- Deprecation warning - unbindFromAll HOT 4
- The page needs to be reload to access new model data in composite view HOT 3
- reorder remove() and _leaveChildren() in leave method under composite_view.js HOT 2
- declaring global Support will fail in strict mode HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backbone-support.