与Knockout observable绑定的Typescript不会更改选定的值

时间:2015-08-13 06:38:33

标签: json knockout.js typescript

在我的打字稿视图模型类中,我对属性有以下结构:

  objectChecks = {
        objVal: {
            customerValue: ko.observable(null),
            exportValue: ko.observable(null),
            myTimestamp1: ko.observable(null),
            rfqStatusValue: ko.observable(null),
            rfqCommentValue: ko.observable(null),
            myTimestamp2: ko.observable(null),
            termConditionValue: ko.observable(null),
            indemnityValue: ko.observable(null),
            myTimestamp3: ko.observable(null),
            quoteOCMValue: ko.observable(null),
            //checkpointValue: ko.observable(null),
            dtReasonValue: ko.observable(null),
            myTimestamp4: ko.observable(null)
        },
        objCom: {
            termCondCommentValue: ko.observable(null),
            quoteComm: ko.observable(null),
            // checkpointComm: ko.observable(null),        
        }
    };

我保存后,作为一个json进入一个字段,我无法在UI中更改值,就像系统从缓存内存更新到保存的值。

保存json数据:

  var self = this;
    //self.objectChecks;
    // format json to ignore null values
    var json = ko.toJSON(self.objectChecks, function (key, value) {
        if (value == null || key === "__ko_mapping__" || key === "__proto__") {
            return;
        }
        else if (value == "") {
            return;
        }
        else {
            return value;
        }
    });

从Json加载:

var jsonFormat = this.commentsAction.replace(/"/g, '"').replace(/(/g, '(').replace(/)/g, ')').replace(/\r\n/g, '\\r\\n').replace(/\t/g, '\\\\t').replace(/'/g, "'").replace(/#/g, '#').replace(/&#36/g, '$').replace(/%/g, '%').replace(/&/g, '&').replace(/*/g, '*').replace(/+/g, '+').replace(/,/g, ',').replace(/-/g, '-').replace(/./g, '.').replace(///g, '/');
        ko.mapping.fromJSON(jsonFormat, {}, this.objectChecks);

查看示例:

<tr>
                <td class="fl"><p>Terms & Conditions?</p></td>
                <td class="fv"><select class="inputControlFlexControl" data-bind="options: termConditionField, value: objectChecks.objVal.termConditionValue, optionsCaption: 'Select...', event:{change: saveButton}"></select><span class="ui-icon ui-icon-help" style="display:inline" /></td>

                <td><!--used only for structure--></td>

                <td class="fl flu"><p>Requirement?</p></td>
                <td class="fv fvu"><select class="inputControlFlexControl" data-bind="options: nuclearIndemnity, value: objectChecks.objVal.indemnityValue, optionsCaption: 'Select...', event:{change: saveButton}"></select><span class="myQuestCs" /></td>

            </tr>
            <tr>
                <!-- <tr data-bind="visible: check13Selection()">-->
                <td class="fl flu"><p>Comments</p></td>
                <td class="fv fvu"><textarea class="iled inlineMLTxtEditorLinHeight" data-bind="value: objectChecks.objCom.termCondCommentValue, event:{change: saveButton}" style="width: 50%" /></td>
            </tr>

知道可能产生此问题的原因吗?

经过测试,我发现问题来自于Json的负载:&#34;我改变值并更新到服务器上保存的值后运行的方法,所以我会添加一个标志,不再运行它?

1 个答案:

答案 0 :(得分:0)

我的解决方案是添加一个用作标志的属性,因为这将限制每次从服务器获取保存的值:

saveFlag = ko.observable(0);
if (this.saveFlag() === 0) {
        var jsonFormat = this.commentsAction.replace(/&#34;/g, '"').replace(/&#40;/g, '(').replace(/&#41;/g, ')').replace(/\r\n/g, '\\r\\n').replace(/\t/g, '\\\\t').replace(/&#39;/g, "'").replace(/&#35;/g, '#').replace(/&#36/g, '$').replace(/&#37;/g, '%').replace(/&#38;/g, '&').replace(/&#42;/g, '*').replace(/&#43;/g, '+').replace(/&#44;/g, ',').replace(/&#45;/g, '-').replace(/&#46;/g, '.').replace(/&#47;/g, '/');
        ko.mapping.fromJSON(jsonFormat, {}, this.objectChecks);
        this.preOrderChecks(true);
        this.saveFlag(1);
        $("#BTN_TB_TaskForm_MarkAsCompleted").hide();
        if (epmcrm.step === '') {
            this.stepStatus('Compliance Pre-Qualification');
        }
    }
    else if (this.saveFlag() === 1) {
        $("#BTN_TB_TaskForm_MarkAsCompleted").hide();
        this.preOrderChecks(true);
    }
相关问题