gpt4 book ai didi

javascript - 使用 ko.toJSON 时传递一个返回 ko.compulated Error 值的函数

转载 作者:行者123 更新时间:2023-12-03 09:55:16 28 4
gpt4 key购买 nike

我正在尝试将 View 模型序列化为 JSON 以发送回我的服务器,但收到以下错误。

Uncaught Error: Pass a function that returns the value of the ko.computed

错误发生在 var data = ko.toJSON(self, mapping); 上行和映射是一次失败的尝试,不尝试将“保存”函数转换为 json。我做错了什么?

更新为包含 JSFiddle

var model = {
"LicenseID": "0e73d791-3ce4-e411-88ba-534e57038000",
"UserName": "#My User",
"UserID": "muUserID",
"MacAddress": "4C-0B-BH-23-4V-BC",
"ComputerName": "My User Description",
"CompanyID": "314083b3-223c-415f-910f-dh7c13j45206",
"TimeLog": false,
"Reject": false,
"Companies": [{
"CompanyID": "7d5b63b3-b0f6-47de-b620-b611ede2c277",
"Name": "Company 1",
"Abbreviation": "Com1"
}, {
"CompanyID": "315083b4-223c-415f-910f-dc7c13c45206",
"Name": "Company 2",
"Abbreviation": "Com2"
}],
"LicensedComputers": [{
"LicensingModel": {
"Company": {
"CompanyID": "315083b4-223c-415f-910f-dc7c13c45206",
"Name": "Company 2",
"Abbreviation": "Com2"
},
"LicenseID": "0e73d791-3ce4-e411-88ba-534e570032580",
"UserName": "#My User",
"UserID": "muUserID",
"MacAddress": "4C-0B-BH-23-4V-BC",
"ComputerName": "My User Description",
"TimeLog": false,
"Reject": false,
"LastSuccessfulUse": null,
"CompanyID": "314083b3-223c-415f-910f-dh7c13j45206"
},
"LicensedComputerID": "d3f49e9a-75d4-4584-a52c-911c4e844d59",
"LicenseID": "0e73d791-3ce4-e411-88ba-534e57000000",
"MacAddress": "4C-0B-BE-23-4B-BC",
"ComputerName": "My Computer Description"
}]
};

function ViewModel(model) {
var self = ko.mapping.fromJS(model);

self.save = function() {
var mapping = {
'ignore': ["save"]
}
var data = ko.mapping.toJSON(self, mapping);
$.post("/Licensing/edit", data, function(returnedData) {
// This callback is executed if the post was successful
});
}

return self;

};

var vm = ViewModel(model);
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.js"></script>

<div class="form-horizontal">
<h4>License</h4>

<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">User Name</label>
<div class="col-sm-10">
<input data-bind='value: UserName' class="form-control">
</div>
</div>

<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">User ID</label>
<div class="col-sm-10">
<input data-bind='value: UserID' class="form-control">
</div>
</div>

<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Company</label>
<div class="col-sm-10">
<select data-bind="options: Companies, optionsText: 'Name', optionsValue: CompanyID, value: CompanyID" class="form-control"></select>
</div>
</div>

<div class="form-group">
<div class="col-sm-10">
<div class="checkbox">
<label>
@*
<input data-bind='value: Reject' type="checkbox">*@ Reject
</label>
</div>
</div>
</div>

<h4>Computers</h4>
<table data-bind='visible: LicensedComputers().length > 0' class="datagrid" style="width: 1000px">
<thead>
<tr>
<th>Computer Name</th>
<th>Mac Address</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: LicensedComputers'>
<tr>
<td>
<input class='required' data-bind='value: MacAddress, uniqueName: true' />
</td>
<td>
<input data-bind='value: ComputerName' />
</td>
<td><a href='#' data-bind='click: $root.removeGift'>Delete</a>
</td>
</tr>
</tbody>
</table>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button data-bind="click: save" class="btn btn-default">Save</button>
</div>
</div>
</div>

最佳答案

我将您的代码复制到 fiddle 中并禁用了后操作。我遇到了一个令人困惑的 SecurityError,但发现当我注释掉 HTML 的这一部分时,错误就消失了:

<!--div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Company</label>
<div class="col-sm-10">
<select data-bind="options: Companies, optionsText: 'Name', optionsValue: CompanyID, value: CompanyID" class="form-control"></select>
</div>
</div-->

请注意,此处的 CompanyID 未加引号。您使用模型的 CompanyID 成员作为值字段的名称。当我引用它时,错误就消失了。

http://jsfiddle.net/49etLon9/2/

关于javascript - 使用 ko.toJSON 时传递一个返回 ko.compulated Error 值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com