Skip to content

Instantly share code, notes, and snippets.

@romaricpascal
Last active December 21, 2015 12:58
Show Gist options
  • Save romaricpascal/6309052 to your computer and use it in GitHub Desktop.
Save romaricpascal/6309052 to your computer and use it in GitHub Desktop.
Inherit and extend defaults when extending a Backbone model
var Parent = Backbone.Model.extend({
defaults: function () {
return {
prop: 'value',
anotherProp: 'anotherValue'
}
}
});
var Child = Parent.extend({
defaults: function () {
var parentDefaults = Parent.prototype.defaults;
if (_.isFunction(parentDefaults)) {
parentDefaults = parentDefaults.call();
}
return _.chain(parentDefaults)
.clone()
.extend({
prop: 'newValue',
newProp: 'newValueToo'
})
.value();
}
});
console.log(new Parent().attributes);
console.log(new Child().attributes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment