アイテムを保存するたびに、次のようにモデルを再マップします。
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
配列の内容に基づいて 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 サイトから取得したものです。
これを行う上での助けは大歓迎です! ありがとう!