Communicating with Knockout components

蹲街弑〆低调 提交于 2019-12-11 04:15:52

问题


Is there a way to communicate from the parent viewmodel to a KnockoutJS component?

I have a component that contains a bootstrap modal dialog box, to search for customers and return a selected customer. At present, to make the dialog box appear, I pass an observable boolean from the viewmodel in the component's params attribute. To make the dialog appear I set this to true, which invokes the dialog box. I also pass a callback function in params to return the results.

Here is a fiddle demo which shows the concept: http://jsfiddle.net/Quango/5bxbsLt6/

Passing the observable boolean to invoke the dialog doesn't feel right, but it's the only working solution I have. The only other idea I had was to use ko-postbox to create a publish/subscribe function.

It feels like there should be a way to invoke actions, e.g. component.Show() ?


回答1:


I think the answer here is that there isn't a better way. To communicate from a parent viewmodel to the component, pass an observable value, and then use that directly or subscribe to changes in the component.

I will put a suggestion on the Knockout github project to consider some form of interface.



来源:https://stackoverflow.com/questions/26931029/communicating-with-knockout-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!