我可以从其他绑定中的值中敲除生成绑定吗? [英] Can I make knockout generate bindings from the values in other bindings?
问题描述
在询问此问题并得到答案我有一个模式形式,其内容数据绑定到一个淘汰模型。这是伟大的,使它通用和可重用,只要表单遵循与模板相同的模式,即几个按钮和一个固定的身体。
After asking this question and getting an answer I have a modal form which has its contents data bound to a knockout model. this is great and makes it generic and reusable, so long as the form follows the same pattern as template, ie a couple of buttons and a fixed body.
我会就像使我的视图模型中的动态并且包含与其他值绑定的输入字段一样。
What I would like is to make the body dynamic and contain input fields bound to other values on my view model.
所以我有这个:
<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">×</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模型看起来像这样:
which extends the form to include a couple of text input fields. Can I make this body content so that it is still populated from my view model, but still has the data-bindings. So have a vierw model that looks like this:
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);
}
}
并让模板回到这样的通用
and have the template go back to being generic like this
<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">×</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
but still have knockout bind changes to the first input to paramName
and changes to the second input to paramType
推荐答案
我将使用动态模板处理这一点,如说明5 here 所述。所以它看起来像这样:
I would handle this with a dynamic template, as described in note 5 here. So it would look something like this:
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>
然后分别定义所有需要的模板:
and then of course define all of your needed templates separately:
<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-bound html保留在字符串中。
Not quite what you were looking for, but I think that'll be a lot easier and maintainable than trying to keep all your various ko-bound html in strings throughout your code.
这篇关于我可以从其他绑定中的值中敲除生成绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!