javascript - Pass a function that returns the value of the ko.computed Error when using ko.toJSON -
i trying serialize viewmodel json send server , receiving following error.
uncaught error: pass function returns value of ko.computed
the error happens on var data = ko.tojson(self, mapping); line , mapping failed attempt not try , convert 'save' function json. doing wrong?
updated include 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) { // callback executed if post 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>
i copied code fiddle , disabled post action. got perplexing securityerror, found when commented out section of html, error went away:
<!--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-->
note companyid not in quotes here. you're using companyid member of model name of value field. when quoted it, error went away.
Comments
Post a Comment