0

我在主 viewModel - QuoteSimpleSearchVM中有一个名为simpleSearchResultsArray的数组。

数组simpleSearchResultsArray包含SimpleSearchResults对象。

在每个SimpleSearchResults中定义了一个名为quoteDetailsObj的对象。数据被添加到quoteDetailsObj中。并且对象quoteDetailsObj绑定到视图。我无法将此嵌套数组视图模型绑定到视图。这是 jsfiddle 的链接:http: //jsfiddle.net/FENuV/29/

查看代码:

<div id="QdetailsDiv">
    <div>
        <div>
            <div><span>Sold to Account</span></div>
            <div>
                <input type="text" id="txtSoldToAccount" data-bind="value:SoldToAccount"/>
            </div>
        </div>
        <div>
            <div><span>Job Name</span></div>
            <div>
                <input type="text" id="txtQDJobName"  data-bind="value:jobName"/></div>
        </div>
    </div>
    <input type="button" value="unbind" id="unbindbutton"></input>
</div>

视图模型代码:

QuoteDetails = function () {
    var self = this;
    self.SoldToAccount = ko.observable();
    self.jobName = ko.observable();
};

function SimpleSearchResults() {
    var self = this;
    self.index = ko.observable();
    self.quoteDetailsObj= null; 

    self.BindDataToQuotesDetailVM = function (SimpleSearchResultsObj) {
        SimpleSearchResultsObj.quoteDetailsObj = ko.observable(new QuoteDetails()); 
        SimpleSearchResultsObj.quoteDetailsObj.SoldToAccount = ko.observable("SoldToAccount test");        
        SimpleSearchResultsObj.quoteDetailsObj.jobName = ko.observable("jobName test");
        ko.applyBindings(SimpleSearchResultsObj.quoteDetailsObj, document.getElementById("QdetailsDiv"));
    };
};

function QuoteSimpleSearchVM() {

    var self = this;
    self.simpleSearchResultsArray = ko.observableArray([]);

    var SimpleSearchResultsObj= new SimpleSearchResults();
    SimpleSearchResultsObj.index=ko.observable("1");
    self.simpleSearchResultsArray.push(SimpleSearchResultsObj);    

    var SimpleSearchResultsObj2= new SimpleSearchResults();
    SimpleSearchResultsObj2.index=ko.observable("2");
    self.simpleSearchResultsArray.push(SimpleSearchResultsObj2);    

};
 var obj= new QuoteSimpleSearchVM();
$(document).ready(function () {

    $("#unbindbutton").click(function(){
        var element = $('#QdetailsDiv')[0];
        ko.cleanNode(element);
    });    

    ko.utils.arrayForEach(obj.simpleSearchResultsArray(), function (SimpleSearchResultsObj) {             
                if (SimpleSearchResultsObj.index() === "2") {
                    SimpleSearchResultsObj.BindDataToQuotesDetailVM(this);
                }
            });
});

谁能帮我解决这个问题?

提前致谢。

4

1 回答 1

1

你打电话时

SimpleSearchResultsObj.BindDataToQuotesDetailVM(this);

“this”指的是窗口,我不认为那是你想要做的。我想您想引用当前的“SimpleSearchResults”,为什么不在BindDataToQuotesDetailVm 中使用“self”?

self.BindDataToQuotesDetailVM = function () {
    self.quoteDetailsObj = new QuoteDetails(); 
    self.quoteDetailsObj.SoldToAccount("SoldToAccount test");        
    self.quoteDetailsObj.jobName("jobName test");
    ko.applyBindings(self.quoteDetailsObj, document.getElementById("QdetailsDiv"));
};

此外,您在“BindDataToQuotesDetailVm”中所做的事情是错误的。"QuoteDetails" 声明了 observable,你在函数中重新分配了新的 observable。它行不通。您必须为现有的 observables 赋值(见上文)。这是一个工作示例:http: //jsfiddle.net/ynV35/

于 2013-07-19T06:19:48.490 回答