VJAI
VJAI

Reputation: 32758

Achieving inheritance in knockoutjs components

I've been going through the Knockoutjs components (http://knockoutjs.com/documentation/component-overview.html) for creating re-usable widgets. I've searched enough but couldn't able to find an example of how to achieve inheritance. For example let's say you've a base window component and other windows can inherit from it and override the methods. Is this possible in Knockout?

Upvotes: 0

Views: 53

Answers (1)

Roy J
Roy J

Reputation: 43881

"Favor composition over inheritance" is often said, but seldom practiced. I think it should come into play here. Inheritance should be used for extension. If you are overriding methods, you should favor composition.

Components are, themselves, composites of objects (their viewmodels) and templates, rather than traditional objects with data and methods.

Clearly you can take the viewmodel portion and inherit from it or compose a new viewmodel that incorporates it. Create a new component based on the new viewmodel, using either the old template or a new one (I don't see a sensible way to inherit from a template, although you could compose a new template that incorporates the old one).

In short, take the template and the viewmodel separately instead of trying to inherit from the component.

Upvotes: 1

Related Questions