可我做的淘汰赛产生从其他的绑定值绑定?(Can I make knockout generate b

2019-10-19 14:30发布

找来找去这个问题并得到答案我有具有结合到淘汰赛模型其内容的数据模式窗体。 这是伟大的,使得它的通用和可重用的,只要形式遵循相同的模式为模板,即一对夫妇按钮和一个固定机构。

我想是使身体充满活力,包含结合其它值,我的视图模型输入字段。

所以我有这样的:

<script id="myModal" type="text/html">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-bind="click:close" aria-hidden="true">&times;</button>
                <h3 data-bind="html:header"></h3>
            </div>
            <div class="modal-body">
                Name:<input type="text" data-bind="value: paramName" /><br/>
                Type:<input type="text" data-bind="value: paramType" /><br />
            </div>
            <div class="modal-footer">
                <a href="#" class="btn" data-bind="click:close,html:closeLabel"></a>
                <a href="#" class="btn btn-primary" data-bind="click:action,html:primaryLabel"></a>
            </div>
        </div>
    </div>
</script>

其延伸的形式为包括一对夫妇文本输入字段。 我可以做出这样的主体内容,以便它仍然是从我的视图模型填充,但仍然有数据绑定。 所以有一个vierw模型,看起来像这样:

modal = {
    header: ko.observable("This is a modal"),
    body: ko.observable("Name:<input type='text' data-bind='value: paramName' /><br/>Type:<input type='text' data-bind='value: paramType' /><br />"),
    paramName: ko.observable(),
    paramType: ko.observable(),
    closeLabel: "Cancel",
    primaryLabel: "Ok",
    show: ko.observable(false), /* Set to true to show initially */
    onClose: function () {
        self.onModalClose();
    },
    onAction: function () {
        if (self.modal.paramName() && self.modal.paramType()) {
            self.nextParameter.name(self.modal.paramName());
            self.nextParameter.type(self.modal.paramType());
            self.addInputParameter();
            self.modal.show(false);
        }            
    }

并在模板回去做一般像这样

<script id="myModal" type="text/html">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-bind="click:close" aria-hidden="true">&times;</button>
                <h3 data-bind="html:header"></h3>
            </div>
            <div class="modal-body" data-bind"html: body">
            </div>
            <div class="modal-footer">
                <a href="#" class="btn" data-bind="click:close,html:closeLabel"></a>
                <a href="#" class="btn btn-primary" data-bind="click:action,html:primaryLabel"></a>
            </div>
        </div>
    </div>
</script>

但仍然有敲除绑定改变到第一输入paramName和改变到第二输入paramType

Answer 1:

如附注5描述我将与动态模板处理这个问题, 在这里 。 因此,这将是这个样子:

modal = {
    header: ko.observable("This is a modal"),
    //this is now just the name of the template
    body: ko.observable('bodyTemplateA'),
    // ...
};

然后在你的绑定,这样做

<div class="modal-body" data-bind="template: { name: body }">
</div>

然后当然定义你所有的需要的模板分开:

<script id="bodyTemplateA" type="text/html">
     Name:<input type="text" data-bind="value: paramName" /><br/>
     Type:<input type="text" data-bind="value: paramType" /><br />
</script>

不太你要找的,但我认为这将是一个很多比试图让所有的各种字符串中KO结合HTML整个代码更容易和可维护性。



文章来源: Can I make knockout generate bindings from the values in other bindings?