gpt4 book ai didi

binding - knockout 动态绑定(bind)问题

转载 作者:行者123 更新时间:2023-12-02 00:10:49 26 4
gpt4 key购买 nike

考虑以下通过 knockout 映射插件生成的 ViewModel。

var originalData = {

"QuoteSelectedViewModel": {
"ProductName": "Select",
"CoverQuotesViewModel": [
{
"Code": 1,
"Label": "Première Assistance 24h/24 (GRATUITE)",
"IsMandatory": true,
"IsSelected": true,
"DependsOn": []
},
{
"Code": 2,
"Label": "Assistance PLUS 24h/24",
"IsMandatory": false,
"IsSelected": false,
"DependsOn": []
},
{
"Code": 8,
"Label": "Heurts Animaux / Force de la Nature",
"IsMandatory": false,
"IsSelected": false,
"DependsOn": [
2
]
},
]}
}

var viewModel = ko.mapping.fromJS(originalData);

ko.applyBindings(viewModel);


<div data-bind="with: QuoteSelectedViewModel">
selected quote is : <span data-bind="text: ProductName"></span>
<!-- ko foreach: CoverQuotesViewModel -->
<br/>
<div data-bind: if: IsVisible>
<input type="checkbox" data-bind="checked: IsSelected"></input>
<input type="text" data-bind="value: Label, enable: IsSelected"></input>
</div>
<!-- /ko -->
</div>

现在,我想在 IsVisible 返回 false 时隐藏 div。 IsVisible 尚不存在,它应该是 CoverQuotesViewModel 数组的每个元素上的计算可观察函数。

如何在每个元素上生成这个计算的可观察函数?

谢谢

[编辑]我在这里添加了一个 jsfiddle:http://jsfiddle.net/graphicsxp/fpKWM/

[编辑2]实际上, knockout 文件清楚地说明了如何做到这一点:

Of course, inside the create callback you can do another call to ko.mapping.fromJS if you wish. A typical use-case might be if you want to augment the original JavaScript object with some additional computed observables:

var myChildModel = function(data) { ko.mapping.fromJS(data, {}, this);

this.nameLength = ko.computed(function() {
return this.name().length;
}, this); }

[编辑]

以下是遵循 Paul 建议的完整代码:(getQuotesSuccess 是一个 AJAX 成功处理程序)

viewModel.getQuotesSuccess = function (result) {
var myCoverQuotesViewModel = function (data, parent) {
var self = this;
ko.mapping.fromJS(data, {}, this);

self.IsVisible = ko.computed(function () {
var visible = true;
if (self.DependsOn().length > 0) {
$.each(self.DependsOn(), function (index, value) {
var dependency = viewModel.QuoteSelectedViewModel().CoverQuotesViewModel.filterByProperty("Code", value);
if (dependency().length > 0) {
visible = visible & dependency()[0].IsSelected();
} else {
visible = false;
}
});
}

return visible;

}, this);
}

var mapping = {
'CoverQuotesViewModel': {
create: function (options) {
return new myCoverQuotesViewModel(options.data, options.parent);
}
}
}

ko.mapping.fromJS(result, mapping, viewModel);
};

最佳答案

好吧,回到我之前的答案,加上你的修改,所以其他看到这个答案的人实际上得到了正确的版本!

您需要创建一个子 viwe 模型,并使用映射插件自动填充它,然后添加到您的计算可观察对象中:

function CoverQuotesViewModel(data)
{
var self = this;
ko.mapping.fromJS(data, {}, self);

// Copy the data to each property.
self.IsVisible = ko.computed(function()
{
// your logic for each quote
});
}

然后您需要使用一个创建映射来映射主视图模型,并在此创建您的 subview 模型:

var mapping = {
'CoverQuotesViewModel': {
create: function(options) {
var model = new CoverQuotesViewModel(options.data);
return model;
}
}
}
var viewModel = ko.mapping.fromJS(data, mapping);

您不需要将 this 传递给计算,因为您引用的是 self,这是您存储的 this 版本。

关于binding - knockout 动态绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com