如何使用KnockoutJS删除不需要的JSON节点

时间:2013-08-13 05:59:25

标签: json knockout.js

我在发布json数据时遇到问题,因为我不知道如何在ajax回发期间删除其中一个json节点(在本例中为ServiceOptions)。

首先这是我的HTML:

<h3>Company Profile:</h3>
<ol style="list-style:none;">
    <li>
        Company Name<br />
        <input id="CompanyName" type="text" data-bind="value:CompanyName" />
    </li>
    <li>
        Service Type<br />
        <select id="ServiceType" data-bind="options:ServiceOptions,optionsText:'text',optionsValue:'value',value:ServiceType,optionsCaption:'Choose....'">
        </select>
    </li>
    <li>
        Street<br />
        <input id="Street1" type="text"  data-bind="value:Street1" /><br />
        <input id="Street2" type="text"  data-bind="value:Street2" /><br />
        <input id="Street3" type="text"  data-bind="value:Street3" />
    </li>
    <li>
        Suburb<br />
        <input id="Suburb" type="text" data-bind="value:Suburb" />
    </li>
    <li>
        Post Code<br />
        <input id="PostCode" type="text" data-bind="value:PostCode" />
    </li>
    <li>
        State<br />
        <input id="State" type="text" data-bind="value:State" />
    <li>
        Telephone<br />
        <input id="Telephone" type="text" data-bind="value:Telephone" />
    </li>
    <li>
        Fax<br />
        <input id="Fax" type="text" data-bind="value:Fax" />
    </li>
    <li>
        Status <input id="IsActive" type="checkbox" data-bind="checked: IsActive" />
    </li>
    <li>
        <button data-bind="click:Update_Click">Update</button>
        <button data-bind="click:Delete_Click">Delete</button>
    </li>
</ol>

我的javascript是:

    var model = function () {
        var self = this;
        self.CompanyName = ko.observable('');
        self.ServiceType = ko.observable();
        self.ServiceOptions = ko.observableArray(   
            [
                { value: 0, text: 'Dry Cleaning' },
                { value: 1, text: 'Dog Walking' }
            ]
        );
        self.Street1 = ko.observable('');
        self.Street2 = ko.observable('');
        self.Street3 = ko.observable('');
        self.Suburb = ko.observable('');
        self.PostCode = ko.observable('');
        self.State = ko.observable('');
        self.Telephone = ko.observable('');
        self.Fax = ko.observable('');
        self.IsActive = ko.observable(false);
        this.Update_Click = function () {
            alert(ko.toJSON(self));
        };
        this.Delete_Click = function () {
            alert('delete');
        };
    };

    $(document).ready(function () {
        ko.applyBindings(new model);
    });

当我点击更新按钮时,我正在

“CompanyName”:“”,“ServiceOptions”:[{“value”:0,“text”:“Dry Cleaning”},{“value”:1,“text”:“Dog Walking”}], “Street1”: “”, “STREET2”: “”, “Street3”: “”, “郊区”: “”, “邮编”: “”, “国家”: “”, “电话”: “”,”传真 “:””, “IsActive”:假}

我仍在获取ServiceOptions,您可以在上面的JSON结果中看到。

知道怎么解决吗?

1 个答案:

答案 0 :(得分:3)

ko.toJSON在内部使用JSON.stringify,您可以在其中指定replacer funciton。在此替换器函数中,您需要返回undefined以获取您不想包含在最终JSON中的键(属性名称):

this.Update_Click = function () {
    alert(ko.toJSON(self, function(key, value) { 
         if (key == "ServiceOptions") 
             return undefined;
         return value;
    }));
};