call function for child component

23 views
Skip to first unread message

aleksand...@rokolabs.com

unread,
Jun 8, 2018, 10:19:33 AM6/8/18
to KnockoutJS
I have 4 components, each component has save function. 
But Save Button located in parent component and should call save function for child component.
Child components locate on different routes. 
As I think, I should have Save Button for each child component. 
And Save Button should know, which child component should to call.
How I can to call Save function for child component?

SLG

unread,
Jun 8, 2018, 11:39:19 AM6/8/18
to KnockoutJS
It is not recommended to call child component save function directly. It is a violation of seperation of concern. 

I would suggest you use some sort of pub/sub model.

On you root component, you save function does nothing but raise an event called "NeedToSave"

All child component will subscribe this event and call the save function of its own.

For light weighted library you can use rniemeyer's excellent kopostbox  https://github.com/rniemeyer/knockout-postbox
For more feature rich library, you can use https://github.com/postaljs
Reply all
Reply to author
Forward
0 new messages