Knockout JS Uncaught TypeError: Cannot read property 'fromJS' in Oracle Cloud
Knockout JS Uncaught TypeError: Cannot read property 'fromJS' in Oracle Cloud
我在 Knockout.js 中收到此错误:
Uncaught TypeError: Cannot read property 'fromJS' of undefined
我是 Knockout 的新手 JS.I 正在 Oracle Content and Experience Cloud 中开发 Knockout JS。我的场景是我正在尝试调用 Rest API 并获取结果并将其显示在 table.
中
我正在使用下面的 REST API url 来测试我的应用程序
http://learn.knockoutjs.com/mail?folder=inbox
下面是我的代码:
/* 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 解决了它,并将代码中的 this 替换为 self
我在 Knockout.js 中收到此错误:
Uncaught TypeError: Cannot read property 'fromJS' of undefined
我是 Knockout 的新手 JS.I 正在 Oracle Content and Experience Cloud 中开发 Knockout JS。我的场景是我正在尝试调用 Rest API 并获取结果并将其显示在 table.
中我正在使用下面的 REST API url 来测试我的应用程序 http://learn.knockoutjs.com/mail?folder=inbox
下面是我的代码:
/* 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 解决了它,并将代码中的 this 替换为 self