If you want to use Backbone.js’s extend function on your own class, all you have to do is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var LightModel = function (attributes, options) { this .attributes = _.extend({}, attributes); } _.extend(LightModel.prototype, Backbone.Events, { get: function (key) { return this .attributes[key]; }, set: function (key_or_values, value) { if ( typeof key_or_values == 'string' ) { this .attributes[key_or_values] = value; } else { _.extend( this .attributes, key_or_values); } } }); LightModel.extend = Backbone.extend; |
Backbone’s extend function is the same between Backbone’s classes. It just uses the prototype it was called with. MyModel1 and MyModel2 in the following example would be equivalent:
1 2 | var MyModel1 = Backbone.Model.extend({}); var MyModel2 = Backbone.View.extend.call(Backbone.Model, {}); |