简体   繁体   English

在可计算的可观察对象上剔除JS绑定不起作用

[英]Knockout JS binding on computed observable does not work

I am trying to add a new field to an asp.net MVC 5 website popup screen that uses Entity Framework 6 code first, Typescript and Knockout JS for databinding. 我正在尝试向asp.net MVC 5网站弹出屏幕添加一个新字段,该屏幕首先使用Entity Framework 6代码,Typescript和Knockout JS进行数据绑定。 I did not write this website. 我没有写这个网站。 I have been making changes to it for a few months. 我已经对其进行了几个月的更改。 The original programmer is no longer with the company. 原来的程序员不再在公司任职。 I have never worked with these technologies previously. 我以前从未使用过这些技术。

The new field is the result of a web service call. 新字段是Web服务调用的结果。 The web method does return results. Web方法不会返回结果。 However, the value is not displayed on the screen. 但是,该值不显示在屏幕上。 I script runs and displays all the other data. 我运行脚本并显示所有其他数据。 The deferred call to the web service returns after the page displays. 页面显示后,将返回对Web服务的延迟调用。 I will provide the markup and view model code. 我将提供标记并查看模型代码。 Any advice is greatly appreciated. 任何意见是极大的赞赏。

Below is the computed property that the HTML is bound to: 以下是HTML绑定到的计算属性:

this.PredictedValue = ko.pureComputed(() => {
                var age = "";
                var race = "";
                var height = "";
                var studyId = this.Session().Study.PftCentralStudyId();
                var predictedSetName;
                var predictedSetId;
                var gender;
                if (this.StudyTestParameter().HasPredictedValues() == true) {
                    ko.utils.arrayForEach(this.Session().Study.StudyTestTypePredictedSets(),(item: Bll.TestTypePredictedSetVm) => {
                        if (String(item.TestType().Name()) == this.StudyTestParameter().TestType().Name())
                            predictedSetId = item.PredictedSetId();
                    });
                    if (predictedSetId == 0) {
                        return "";
                    }
                    else {
                        var match = ko.utils.arrayFirst(this.Session().PftCentralStudyPredictedSets(),(item: Bll.PftCentralPredictedSetsVm) => {
                            return String(item.Id) == String(predictedSetId)
                        });
                        predictedSetName = match.Name;
                        ko.utils.arrayForEach(this.Session().SessionValues(),(item: SessionValueVm) => {
                            if (String(item.StudySessionParameter().Name()) == "Age")
                                age = String(item.RecordedValue());
                        });
                        ko.utils.arrayForEach(this.Session().SessionValues(),(item: SessionValueVm) => {
                            if (String(item.StudySessionParameter().Name()) == "Race")
                                race = String(item.RecordedValue());
                        });
                        ko.utils.arrayForEach(this.Session().SessionValues(),(item: SessionValueVm) => {
                            if (String(item.StudySessionParameter().Name()) == "Height")
                                height = String(item.RecordedValue());
                        });
                        ko.utils.arrayForEach(this.Session().SessionValues(),(item: SessionValueVm) => {
                            if (String(item.StudySessionParameter().Name()) == "Sex")
                                gender = String(item.RecordedValue());
                        });
                        var promise = this.Session().CalculatePredicted(age, race, gender, height, String(this.StudyTestParameter().PftCentralStudyParameterId()), predictedSetName, studyId);
                        promise.done((data: string) => {
                            return data
                        });
                    }
                }
                else
                    return "";
            });

CalculatePredicted = (age: string, race: string, gender: string, height: string, studySessionParameterId: string, predictedSetName: string, studyId: number) => {

            var deferred = $.Deferred();

            $.ajax({
                url: "/Workflows/CalculatePredicted",
                cache: false,
                data: { age: age, ethnicity: race, gender: gender, height: height, studySessionParameterId: studySessionParameterId, testTypePredictedSetName: predictedSetName, studyId: studyId },
                dataType: "json",
                contentType: "application/json charset=utf-8"
            }).done(data => {
                deferred.resolve(data);
            }).fail((jqXHR) => {
                alert(jqXHR.responseText);
                deferred.reject();
            });
            return deferred;
        }

Below is the HTML. 以下是HTML。

<div>
                                        Test Values:
                                        <table class="width100pct gridtable">
                                            <tbody data-bind="foreach: TestValues">
                                            <tr>
                                                <td data-bind="text: StudyTestParameter().Name"></td>
                                                <td data-bind="text: RecordedValue"></td>
                                                <td data-bind="text: ATSBestValue"></td>
                                                <td data-bind="text: PredictedValue"></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>

your promise object can't return for your computed. 您的Promise对象无法返回您的计算值。 By the time the promise is done, the computed has long returned 'undefined'. 到完成承诺时,计算所得的内容早就返回了“未定义”。 That is the nature of async calls. 这就是异步调用的本质。 Consider setting a different observable within the promise.done() function and bind to that new field in the UI instead; 考虑在promise.done()函数中设置一个不同的observable,然后绑定到UI中的该新字段; the computed function will still trigger if the underlying fields change. 如果基础字段发生更改,则计算函数仍将触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM