-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defaults don't propagate through multiple levels of inheritance #1244
Comments
var child = parent.extend({
defaults: _.extend(parent.prototype.defaults, {
bar: 2
})
}); note that parent.extend and _.extend are not the same methods. Also see #276 why this has been declared as the Backbone way. note that as defaults can be declared as a function you might need something like the following if you're inheriting multiple levels var child = parent.extend({
defaults: function () {
var defaults = parent.prototype.defaults;
var d = _.isFunction(defaults) ? defaults() : defaults;
return _.extend(d, {
bar: 2
})
}
}); |
As a workaround you can assign a function to the defaults attribute and extend that: Animal = Backbone.Model.extend({
defaults: {
eyes: 2,
legs: 4
}
});
Dinosaur = Animal.extend({
defaults: function() {
return _.extend({
legs: 2,
arms: 'too-short'
}, _.result(Dinosaur.__super__, 'defaults'));
},
});
var dog = new Animal();
console.log('Dog has:', dog.get('eyes'), 'eyes and', dog.get('legs'), 'legs');
var rex = new Dinosaur();
console.log('Rex has:', rex.get('eyes'), 'eyes and', rex.get('legs'), 'legs'); Will output:
|
Hi @captncraig. While |
Consider this code:
If you extend an existing model and provide new defaults, the original defaults are lost and don't operate as would be intended.
This could possibly be fixed by modifying extend to perform some basic merging with each member of the defaults for both the parent and the child.
The text was updated successfully, but these errors were encountered: