0

我在定义的对象上有一个子可观察数组。当来自服务器的对象被返回并填充到淘汰赛 observable 中时,数据无法显示。但是可以通过调试来验证视图模型中的数据。我创建了说明问题的 jsFiddle。

http://jsfiddle.net/CraigHead/Y2Fr6/

我的视图模型是这样定义的

function PropertyLease(propertyLeaseId, propertyCatalogId, confirmedBldgSF, leaseExpenseStructure, leaseTermRate, propertyType, yearBuilt)
{
    var self = this;
    self.PropertyLeaseId  = ko.observable(propertyLeaseId);
    self.PropertyCatalogId = ko.observable(propertyCatalogId);
    self.ConfirmedBldgSF = ko.observable(confirmedBldgSF);
    self.LeaseExpenseStructure = ko.observable(leaseExpenseStructure);
    self.LeaseTermRate = ko.mapping.fromJS(leaseTermRate);
    self.PropertyType = ko.observable(propertyType);
    self.YearBuilt = ko.observable(yearBuilt);
}

function PropertyLeaseTermRate(propertyLeaseTermId, propertyLeaseId, from, to, annualRatePSF)
{
    var self = this;
    self.PropertyLeaseTermId = ko.observable(propertyLeaseTermId);
    self.PropertyLeaseId = ko.observable(propertyLeaseId);
    self.From = ko.observable(from);
    self.To = ko.observable(to);
    self.AnnualRatePSF = ko.observable(annualRatePSF);
}

当数据从服务器返回时我会更新它们

viewModel.LeaseSearchResults.removeAll();
var vmLeaseSearchResults = viewModel.LeaseSearchResults();
for(var idx = 0; idx<result.LeaseSearchResults.length; idx++){
    var data = result.LeaseSearchResults[idx];
    vmLeaseSearchResults.push(
        new PropertyLease(
            data.PropertyLeaseId, 
            data.PropertyCatalogId, 
            data.ConfirmedBldgSF, 
            data.LeaseExpenseStructure, 
            data.LeaseTermRate, 
            data.PropertyType, 
            data.YearBuilt
            )
        )
}
viewModel.LeaseSearchResults.valueHasMutated();

我不确定我做错了什么?精氨酸!

PropertyLeaseTermRatePS在用户从数据网格中做出选择之前,不会使用第二个视图模型。;-) 很抱歉让您感到困惑。

4

1 回答 1

1

您有多个问题:

  • 如果你是var vmLeaseSearchResults = viewModel.LeaseSearchResults();因为()你正在引用底层数组而不是可观察数组本身而写作。因此,当您拨打vmLeaseSearchResults.pushKnockout 时不会收到通知,因此您需要拨打电话valueHasMutated

    但正确的方法是让 Knockout 进行更改跟踪,因此您应该删除(): 这样var vmLeaseSearchResults = viewModel.LeaseSearchResults;您就不需要valueHasMutated调用了。

  • 你有同样的问题var vmLeaseTermRate = viewModel.SelectedLease().LeaseTermRate();

    但是,这里有第二个问题。当您调用viewModel.SelectedLease(value);淘汰赛时,将开始将您的详细信息列表绑定在<div data-bind="with: SelectedLease">. 但是您的模型 ( value)LeaseTermRate尚未定义,因此 Knockout 将在ko foreach: LeaseTermRate.

    要修复它,只需在调用LeaseTermRate之前移动声明:SelectedLease

    var value = ko.mapping.fromJS(item);
    value.LeaseTermRate = ko.observableArray();
    viewModel.SelectedLease(value);
    var vmLeaseTermRate = viewModel.SelectedLease().LeaseTermRate;
    

演示JSFiddle。

于 2013-06-17T20:00:45.823 回答