Last active
December 18, 2015 15:59
-
-
Save GianlucaGuarini/5808435 to your computer and use it in GitHub Desktop.
Easy example to show how to extend an existing Backbone class inheriting its properties to the new children classes
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
/* | |
* | |
* Parent class containing some methods and properties that must be inherited | |
* | |
*/ | |
var ParentClass = Backbone.Model.extend({ | |
initialize: function() { | |
this.set('name','Dear'); | |
}, | |
say_hello: function ( ) { | |
// property set only on the child | |
console.log(this.get('greeting')); | |
} | |
}); | |
/* | |
* | |
* Child class extending the ParentClass | |
* | |
*/ | |
var ChildClass = ParentClass.extend({ | |
initialize: function () { | |
this.constructor.__super__.initialize.apply(this); | |
this.set('greeting','Hello!'); | |
// inherited method | |
this.say_hello(); | |
// inherited property | |
console.log(this.get('name')); | |
// property set later once the class gets instanced | |
console.log(this.get('surname')); | |
} | |
}); | |
new ChildClass({ | |
surname:'Friends' | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check it out the demo http://jsfiddle.net/gianlucaguarini/RuQxL/