【问题标题】:Can I make knockout generate bindings from the values in other bindings?我可以让淘汰赛从其他绑定中的值生成绑定吗?
【发布时间】:2014-03-17 18:52:05
【问题描述】:

在询问this question 并得到答案后,我有一个模态表单,它的内容数据绑定到一个剔除模型。这很棒,而且它具有通用性和可重用性,只要表单遵循与模板相同的模式,即几个按钮和一个固定的主体。

我想要使主体动态并包含绑定到我的视图模型上的其他值的输入字段。

所以我有这个:

<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 的第二个输入的更改

【问题讨论】:

    标签: javascript data-binding knockout.js


    【解决方案1】:

    我会使用动态模板来处理这个问题,如注释 5 here 中所述。所以它看起来像这样:

    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 保留在整个代码中的字符串更容易和可维护。

    【讨论】:

    • 谢谢亚当,这听起来比我想做的要好得多:),我会试一试
    • 使用模板可能是最好的方法(如果您愿意,您也可以动态创建或加载新模板,网上有几个教程)。为了完整起见,我想补充一点,html bindingHandler 明确地不处理子绑定。它甚至在the code 的评论中指出,它打算“防止绑定到动态注入的 HTML(因为开发人员不太可能预料到这一点,而且它具有安全隐患)”。
    • 这很好用,非常感谢。感谢@robertwesterlund 的留言,我会牢记这一点。
    • @AdamRackis 这很棒,只有一个问题是我如何将自动对焦自动聚焦到脚本模板中的一个输入中以在 IE 中工作。 hasfocus: true 在绑定中似乎不起作用
    • @Sam - 不确定。我会提出一个新问题,看看是否有人可以帮助你。
    猜你喜欢
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多