每当我保存我的项目时,我都会重新映射模型,执行以下操作:
ko.mapping.fromJS(data, {}, deal);
我的模型看起来像:
{
"DealId": 0,
"BrokenRules": [
{
"Property": "EndDate",
"Description": "End Date is required."
},
{
"Property": "CustomerId",
"Description": "Customer is required."
},
{
"Property": "LiveState",
"Description": "Live State is required."
},
{
"Property": "WorkState",
"Description": "Work State is required."
}
}
我想div
根据数组的内容在 a 上设置 css 类,BrokenRules
并希望我可以做类似的事情:
<div class="control-group" data-bind="css: { error: BrokenRules.filterByProperty('Property', 'EndDate').length !== 0 }">
<label class="control-label">End Date</label>
<div class="controls">
<input type="text" class="span2" name="EndDate" data-bind="value: EndDate, enable: $index() === 0" />
</div>
</div>
但这似乎不起作用。我的 filterByProperty 在第一次触发时没有任何项目,并且由于某种原因,再也不会触发。
ko.observableArray.fn.filterByProperty = function (propName, matchValue) {
return ko.computed(function () {
var allItems = this(), matchingItems = [];
for (var i = 0; i < allItems.length; i++) {
var current = allItems[i];
if (ko.utils.unwrapObservable(current[propName]) === matchValue)
matchingItems.push(current);
}
return matchingItems;
}, this);
}
filterByProperty 取自 knockoutjs 站点。
对此的任何帮助将不胜感激!谢谢!