Last active
December 21, 2015 12:58
-
-
Save romaricpascal/6309052 to your computer and use it in GitHub Desktop.
Inherit and extend defaults when extending a Backbone model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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