我已经使用带有敲除的自定义绑定来显示 jqueryui 对话框,但我想使用敲除组件功能。
所以,我想写一些类似的东西:
<window params="isVisible: isVisible">
//there will be some html
</window>
后来在代码中的某个地方:
self.isVisible(true); //That would open window
//or
self.isVisible(false); //That would close 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) 应用对话框,我将设置为对话框父节点,那么我的窗口标签将是:
<div><!-- Dialog will be applyed there-->
<window params="isVisible: isVisible">
//there will be some html
</window>
</div>
我认为它会起作用,但是这里不需要额外的 div ......或者这是完成工作的唯一方法?
什么是 knockout.components 方法来做到这一点?谢谢。