如何在编译的打字稿中调用类中的方法?

时间:2017-04-27 15:21:30

标签: javascript typescript ecmascript-6

我在typescript中创建了一些API帮助程序,用于从Microsoft Dynamics 2016读取/写入数据。我需要从与API交互的页面中调用这些帮助程序中的方法。我似乎无法从Web API调用Get()方法获得正确的代码。我需要在我的$("#ExpenseButton").click回调中放入什么代码来调用Get()方法?

从Typescript编译的Javascript代码

var WebAPI;
(function (WebAPI) {
    class ExpenseTransaction extends WebAPI.APIBase {
        constructor() {
            super();
        }
        ConvertToEntity(data) {
            let result = new Array();
            for (let i = 0; i < data.length; i++) {
                let newRecord = new Model.ExpenseTransaction();
                newRecord.ClientID = data[i]["_ccseq_clientid_value"];
                newRecord.ClientNumber = data[i]["ccseq_clientnumber"];
                newRecord.EmployeeFirstName = data[i]["ccseq_employeefirstname"];
                newRecord.EmployeeLastName = data[i]["ccseq_employeelastname"];
                newRecord.EmployeeID = data[i]["_ccseq_employeeid_value"];
                newRecord.ExpenseErrorCode = data[i]["ccseq_expenseerrorcode"];
                newRecord.GeneralLedgerName = data[i]["ccseq_generalledgername"];
                newRecord.GeneralLedgerNumber = data[i]["ccseq_generalledgernumber"];
                newRecord.GroupCode = data[i]["ccseq_groupcode"];
                newRecord.MasterCardPostedDate = data[i]["ccseq_mastercardposteddate"];
                newRecord.MasterCardTransactionDate = data[i]["ccseq_mastercardtransactiondate"];
                newRecord.MasterCardTransactionParentCompany = data[i]["ccseq_mastercardtransactionparentcompany"];
                newRecord.NAVCompanyCode = data[i]["ccseq_navcompanycode"];
                newRecord.NAVEmployeeID = data[i]["ccseq_navemployeeid"];
                newRecord.NAVGeographyCode = data[i]["ccseq_navgeographycode"];
                newRecord.NAVJobClassCode = data[i]["ccseq_navjobclasscode"];
                newRecord.NAVServiceCode = data[i]["ccseq_navservicecode"];
                newRecord.SetID = data[i]["_ccseq_setid_value"];
                newRecord.TransactionAmount = data[i]["ccseq_transactionamount"];
                newRecord.TransactionDate = data[i]["ccseq_transactiondate"];
                newRecord.TransactionDescription = data[i]["ccseq_transactiondescription"];
                newRecord.TransactionType = data[i]["ccseq_transactiontype"];
                newRecord.Vendor = data[i]["ccseq_vendor"];
                newRecord.StateCode = data[i]["statecode"];
                result[i] = newRecord;
            }
            return result;
        }
        Get(expenses) {
            if (Array.isArray(expenses)) {
                if (expenses[0] instanceof Model.ExpenseTransaction) {
                    return null;
                }
            }
            else {
                return Promise.resolve($.ajax({
                    url: this.Connection,
                    type: "GET",
                    contentType: "application/json",
                    dataType: "json",
                }));
            }
        }
        ;
        Create(expenses) {
            for (let i = 0; i < expenses.length; i++) {
                $.ajax({
                    url: this.Connection,
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(expenses[i].toJSON()),
                    success: function (data) {
                        alert("Success");
                    },
                    error: function (data) {
                        alert("error");
                    }
                });
            }
        }
        ;
    }
    WebAPI.ExpenseTransaction = ExpenseTransaction;
})(WebAPI || (WebAPI = {}));

页面的Javascript代码

(WebAPI || (WebAPI = {}));

$(document).ready(function () {
    setupHandlers();
});

function setupHandlers() {
    "use strict";

    $("#ExpenseButton").click(function () {
        // Put Code here
    });
}

我已经尝试过的代码

// Error: Uncaught TypeError: WebAPI.ExpenseTransaction is not a constructor
let ex = new WebAPI.ExpenseTransaction();

// Error: Uncaught ReferenceError: ExpenseTransaction is not defined
let ex = new ExpenseTransaction();

1 个答案:

答案 0 :(得分:0)

我认为你必须创建一个ExpenseTransaction类的实例。

你可以试试像:

function setupHandlers() {
    "use strict";

    $("#ExpenseButton").click(function () {
        var expenseTransaction = new WebAPI.ExpenseTransaction();
        var expenses = null;
        expenseTransaction.Get(expenses).then(function() {
          alert('hello world');
        });
    });
}

您似乎没有自行调用将类添加到WebAPI的函数。

你可以试试像:

(function(WebAPI) {
  // YOUR TYPESCRIPT CODE

})(); <-- Invoke the anonymous function

您的WebAPI对象必定有问题。我在这里创建了一个简化版本的代码,它可以工作。

https://jsfiddle.net/jruizx/aru02gja/