I have tried bind json file with knockoutjs into my custom template. But i got this error in browser console:
Uncaught TypeError: Cannot read property 'fromJS' of undefined
How to avoid this error? Here some following script that i have:
view/frontend/web/js/view/main-component.js
define(['jquery','uiComponent','ko'], function($, Component, ko){
'use strict';
return Component.extend({
defaults: {
template: 'My_Module/index'
},
initialize: function(){
var json = {
"id": 9,
"fullName": "John Doe",
"firstName": "John",
"lastName": "Doe",
"referenceNumber": "BUY-08",
"position": "Buyer",
"type": "Buyer",
"telephone": "028 82 240780",
"email": "[email protected]",
"departmentId": 3,
"departmentName": "DEPT B",
"country": "United Kingdom"
};
var viewModel = {};
function success() {
viewModel = ko.mapping.fromJS(json);
ko.applyBindings(viewModel);
}
success();
},
});
});
view/frontend/web/template/index.html
<div>
Full Name: <input data-bind="value: fullName" /> <br />
Ref: <input data-bind="value: reference" /> <br />
Position: <input data-bind="value: position" /> <br />
Email: <input data-bind="value: email" /> <br />
Dept: <input data-bind="value: departmentName" /> <br />
Country: <input data-bind="value: country" /> <br />
</div>