I am getting this error in Knockout.js:
Uncaught TypeError: Cannot read property 'fromJS' of undefined
I am new to Knockout JS.I am developing Knockout JS in Oracle Content and Experience Cloud. My scenario is I am trying to call Rest API and get the results and display it in table.
I am using below REST API url to test in my application http://learn.knockoutjs.com/mail?folder=inbox
Below is my code:
/* globals define */
define(['knockout', 'jquery', 'text!./knockout.mapping-latest.js', 'text!./mailbox.json', 'css!./css/design.css'], function (ko, $, mapping, css) {
'use strict';
// ----------------------------------------------
// Define a Knockout Template for your component
// ----------------------------------------------
var sampleComponentTemplate =
'<div>' +
'<p><input data-bind="value: searchValue" class="box"/></p>' +
'<button data-bind="click: getCustomers">Knock out Search</button>' +
'</div>' +
'<table>' +
'<thead>' +
'<tr>' +
'<th>From</th>' +
'<th>To</th>' +
'<th>Subject</th>' +
'<th>Date</th>' +
'</tr>' +
'</thead>' +
'<tbody data-bind="foreach: mails">' +
'<tr>' +
'<td data-bind="text: from"></td>' +
'<td data-bind="text: to"></td>' +
'<td data-bind="text: subject"></td>' +
'<td data-bind="text: date"></td>' +
'</tr>' +
'</tbody>' +
'</table>';
// ----------------------------------------------
// Define a Knockout ViewModel for your template
// ----------------------------------------------
var SampleComponentViewModel = function (args) {
this.searchValue = ko.observable("Hi");
this.mails = ko.observableArray();
this.getCustomers = function () {
alert("Inside get customers ");
$.ajax({
type: 'GET',
crossDomain: true,
url: 'documents/folder/F49A137E34CB4B6DFD302FB90A04F4D8CA1E8A3D5B3E/_assets/mailbox.json',
data: JSON.stringify(this.mails),
success: function(data) {
var observableData = ko.mapping.fromJS(data);
var array = observableData();
this.mails(array);
},
error:function(jq, st, error){
alert("Inside Error Method " + error + " jq is " + jq + "st is " + st);
}
});
};
};
我通过初始化self = this解决了这个问题,并在代码中将它替换为self
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.