Javascript 使用knockoutjs组件创建对话框

Javascript 使用knockoutjs组件创建对话框,javascript,knockout.js,knockout-components,Javascript,Knockout.js,Knockout Components,我已经使用带有敲除的自定义绑定来显示jqueryui对话框,但我想使用敲除组件特性 因此,我想写一些类似于: <window params="isVisible: isVisible"> //there will be some html </window> 问题是我不知道如何应用$element.dialog。当我注册敲除组件时,我只能得到这个组件的容器元素,而不能得到注入的元素 ko.components.register('window', {

我已经使用带有敲除的自定义绑定来显示jqueryui对话框,但我想使用敲除组件特性

因此,我想写一些类似于:

<window params="isVisible: isVisible">
    //there will be some html
</window>
问题是我不知道如何应用$element.dialog。当我注册敲除组件时,我只能得到这个组件的容器元素,而不能得到注入的元素

ko.components.register('window', {
        viewModel: {
            createViewModel: function(params, componentInfo) {
                // - 'params' is an object whose key/value pairs are the parameters
                //   passed from the component binding or custom element
                // - 'componentInfo.element' is the element the component is being
                //   injected into. When createViewModel is called, the template has
                //   already been injected into this element, but isn't yet bound.
                // - 'componentInfo.templateNodes' is an array containing any DOM
                //   nodes that have been supplied to the component. See below.

                // Return the desired view model instance, e.g.:
                return new MyViewModel(params);
            }
        },
        template: ...
    });
因此,componentInfo.element是父节点,如果我使用$componentInfo.element应用dialog,我将设置为dialog父节点,那么我的窗口标记将是:

<div><!-- Dialog will be applyed there-->
    <window params="isVisible: isVisible">
        //there will be some html
    </window>
</div>
我认为它会起作用,但在这里额外的div看起来是不必要的。。。或者这是完成任务的唯一方法


什么是knockout.components方法?谢谢。

当我想使用Bootstrap v2“模态”功能构建一个用于托管对话框窗口的淘汰组件时,我也有类似的要求。我在页面的viewModel中使用了一个可观察的布尔值,最初设置为false。在组件初始化之后,没有一种简单的方法可以与组件通信,除非通过params传入可观测值

此可观察对象作为参数传递给参数中的组件,例如

<dialog-window params="visible: showDialog">[content]</dialog-window>
然后,它为“modal”对话框使用了一个特殊的绑定处理程序,但在您的情况下,可以使用data bind='visible:YourParam'属性直接绑定它

然后,主页可以简单地调用showDialogtrue以使对话框可见

希望这有帮助

<dialog-window params="visible: showDialog">[content]</dialog-window>