0

我在 Knockout.js 中收到此错误:

未捕获的类型错误:无法读取未定义的属性“fromJS”

我是 Knockout JS 的新手。我正在 Oracle Content and Experience Cloud 中开发 Knockout JS。我的场景是我正在尝试调用 Rest API 并获取结果并将其显示在表格中。

我正在使用以下 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);
            }
        });
    };

    };
4

1 回答 1

0

我通过初始化 self =this 并在代码中用 self 替换它来解决它

于 2017-08-17T11:27:20.613 回答