@shakedko
IF AN EXPERT SAYS IT CAN'T BE DONE GET ANOTHER EXPERT.
- DAVID BEN-GURION

Extending Backbone model while keeping defaults & parent init method

Hey, didn't have much time to write here (I`m currently working on my own project hoping to release soon).

I was facing with a problem which took me less than few minutes to find the solution for which I wanted to share.

The problem

I was need to extend my Backbone models while keeping on the defaults property and calling the parent init method

Suggested Solutions

After some Googling I found few ways toย achieve a working solution.

Keeping the defaultsย property:

var ExtendedInventory = Inventory.extend({
    defaults: {
        rabit:25
    }
});
_.extend(ExtendedInventory.prototype.defaults, Inventory.prototype.defaults);

OR

var ExtendedInventory = Inventory.extend({
    defaults: _.extend({},Inventory.prototype.defaults,
         {rabit:25}
    )
});

Reference:ย http://stackoverflow.com/questions/6549149/extending-the-defaults-of-a-model-superclass-in-backbone-js

Calling parent init method

MyModel = BaseModel.extend({
    initialize: function() {
        MyModel.__super__.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});

Reference:ย http://stackoverflow.com/questions/8970606/accessing-parent-class-in-backbone

ย 

Summary

The reason I`m posting this post is not only to share what I found is to know if you have better\different suggestions to do it (Maybe something nicer than the above solutions).

Thanks toย Raynosย andย JCorcuera ย 

Shak.

Work In Progress ๐Ÿšง
Discipline