与淘汰赛组件进行通信 [英] Communicating with Knockout components
问题描述
是否可以通过 父视图模型与KnockoutJS组件进行通信?
Is there a way to communicate from the parent viewmodel to a KnockoutJS component?
我有一个包含引导程序模式对话框的组件,用于搜索客户并返回选定的客户.目前,要使该对话框出现,我在组件的params
属性中的viewmodel中传递了一个可观察的布尔值.为了使对话框出现,我将其设置为true,这将调用对话框.我还在params
中传递了一个回调函数以返回结果.
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.
这里是展示概念的小提琴演示: http://jsfiddle.net/Quango/5bxbsLt6/
Here is a fiddle demo which shows the concept: http://jsfiddle.net/Quango/5bxbsLt6/
传递可观察到的布尔值来调用对话框感觉不对,但这是我唯一可行的解决方案.我唯一的另一个想法是使用ko-postbox创建发布/订阅功能.
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.
感觉应该有一种方法来调用动作,例如component.Show()
吗?
It feels like there should be a way to invoke actions, e.g. component.Show()
?
推荐答案
我认为这里的答案是没有更好的方法.要从父视图模型与组件进行通信,请传递一个可观察的值,然后直接使用该值或订阅组件中的更改.
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.
我将在Knockout github项目上提出建议,以考虑某种形式的接口.
I will put a suggestion on the Knockout github project to consider some form of interface.
这篇关于与淘汰赛组件进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!