【发布时间】:2016-06-23 23:32:36
【问题描述】:
我在我的 MVC 应用程序模型上使用 Knockout 的“foreach”函数来加载下拉选项表。当所选的下拉值值更改时,我需要跟踪模型中的更改。
我尝试在模型中的元素上使用 subscribe 选项,但是当它发生变化时,我绑定到它的函数没有触发。我知道有几种方法可以实现此更改,但我更愿意坚持使用这种定义我的函数的格式,如视图底部所示。
型号:
public class ProfileLookAndFeelViewModel : StandardLayoutViewModel
{
public ProfileLookAndFeelViewModel()
{
Form = new FormGroup();
PriceUOMDropdownOptions = new PriceUOM();
UOMInformation = new UOMInformationGroup();
}
public FormGroup Form { get; set; }
public PriceUOM PriceUOMDropdownOptions { get; set; }
public UOMInformationGroup UOMInformation { get; set; }
public class FormGroup
{
public bool FormValueChanged { get; set; }
}
public class PriceUOM
{
public int Id { get; set; }
public String Name { get; set; }
public string Code { get; set; }
}
public class PriceUOMOverride
{
public string ItemDescription { get; set; }
public string TemplateCode { get; set; }
public string UOMDesc { get; set; }
public List<PriceUOM> PriceUOMDropdownOptions { get; set; }
public int SelectedPriceUOM { get; set; }
public int SelectedPriceUOMOriginal { get; set; }
public bool SelectedPriceUOMChanged { get; set; }
}
public class UOMInformationGroup
{
public UOMInformationGroup()
{
UOMs = new List<PriceUOMOverride>();
}
public List<PriceUOMOverride> UOMs { get; set; }
public bool UOMsChanged { get; set; }
}
}
查看:
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Description</th>
<th>Template Code</th>
<th>Default UOM</th>
</tr>
</thead>
<tbody>
<!-- ko foreach: $root.UOMInformation.UOMs -->
<tr>
<td data-bind="text: ItemDescription"></td>
<td data-bind="text: TemplateCode"></td>
<td class="center" >
<select data-bind="options: PriceUOMDropdownOptions, optionsText: 'Code', optionsValue: 'Id', value: SelectedPriceUOM, css: { important: SelectedPriceUOMChanged() == true }"></select>
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
<script type="text/javascript">
$(function () {
var vmProfileLookAndFeel = function () { var self = this; };
vmProfileLookAndFeel = ko.mapping.childrenIndependently($.parseJSON('@Html.RawJsonForKoMapping(Model)'), ["UOMInformation", "Form", "PriceUOMDropdownOptions"]);
vmProfileLookAndFeel.UOMInformation.UOMs.subscribe(function (newValue) {
vmProfileLookAndFeel.ValueChanged();
});
vmProfileLookAndFeel.ValueChanged = function () {
//Do something;
};
ko.applyBindings(vmProfileLookAndFeel);
});
</script>
我尝试了这个建议,但这里的答案不是从 MVC 模型加载数据,而是从硬编码数组中加载
Subscribe to select item from foreach loop
编辑:
我更新了视图以反映将数组添加为可观察的。
这是我用来映射对象的函数
ko.mapping.childrenIndependently = function (jsObject, childrenArray) {
var mapping = {
"ignore": childrenArray
}
var vm = ko.mapping.fromJS(jsObject, mapping);
// handle children
for (var childrenArrayIndex = 0; childrenArrayIndex < childrenArray.length; childrenArrayIndex++) {
if (jsObject.hasOwnProperty(childrenArray[childrenArrayIndex])) {
// if the property is an array, create an objservable array
// and map each child
// else map the property
if ($.isArray(jsObject[childrenArray[childrenArrayIndex]])) {
vm[childrenArray[childrenArrayIndex]] = ko.observableArray();
for (var childObjectArrayIndex = 0; childObjectArrayIndex < jsObject[childrenArray[childrenArrayIndex]].length; childObjectArrayIndex++) {
vm[childrenArray[childrenArrayIndex]].push(ko.mapping.childrenIndependently(jsObject[childrenArray[childrenArrayIndex]][childObjectArrayIndex], childrenArray.slice(childrenArrayIndex)));
}
}
else {
vm[childrenArray[childrenArrayIndex]] = ko.mapping.childrenIndependently(jsObject[childrenArray[childrenArrayIndex]], childrenArray.slice(childrenArrayIndex));
}
}
}
return vm;
};
【问题讨论】:
-
你没有可观察的。您不能只将事物声明为 get/set,它们需要是可观察的。否则 Knockout 不知道去观察他们。
-
你是对的。这似乎是朝着正确方向迈出的一步,但我仍然没有看到函数在选择更改时触发。
-
如果你想在选择改变时发生一些事情,你需要
SelectedPriceUOM成为一个 observable,并且你需要订阅它。 -
当 UOMInformation.UOMs 集合中有 SelectedPriceUOM 列表时,您如何订阅它们?我订阅了 UOM,我认为其中的每个 SelectedPriceUOM 都是可观察的。
-
这是一个切线,但你真的不应该(需要)在使用淘汰赛时做订阅和调用 ValueChanged() 之类的事情。这是一种反模式,如果/当您使用大型和复杂的 Viewmodel 时,它会咬你一口。正确设置您的 observables(计算是您的朋友),并且所有更改通知都会自动处理。
标签: jquery model-view-controller knockout.js knockout-3.2