One of my JavaScript files is responsible for uploading a file to Microsoft Dynamics CRM. This particular JavaScript file makes use of RequireJS to reference another JavaScript file. The referenced JavaScript file, in turn, was compiled from multiple Typescript files which establish a model for storing data and the necessary logic to interact with the Dynamics API. Despite going through the Typescript/RequireJS documentation and related Stack Overflow questions, I am struggling to correctly implement the define statement in RequireJS to enable me to utilize my model and Dynamics API logic from my JavaScript file. Am I exporting my classes from Typescript correctly? Have I defined my import in JavaScript accurately?
Model.ts
export class ExpenseTransaction extends ModelBase {
public Field1: string;
public Field2: number;
.
.
.
constructor() {
super();
}
toJSON(): any {
let json = {
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0566667660745a436c60696134456a616471642b676c6b61">[email protected]</a>": this.Connection + "(" + this.Field1 + ")",
"ccseq_Field2": this.Field2,
.
.
.
};
return json;
}
}
WebAPI.ts
import * as Model from './Model';
export class ExpenseTransaction extends APIBase {
constructor() {
super();
}
ConvertToEntity = (data: Array<Object>): Array<Model.ExpenseTransaction> => {
let result: Array<Model.ExpenseTransaction> = new Array<Model.ExpenseTransaction>();
for (let i: number = 0; i < data.length; i++) {
let newRecord: Model.ExpenseTransaction = new Model.ExpenseTransaction();
newRecord.Field1 = data[i]["_Field1_value"];
newRecord.Field2 = data[i]["ccseq_Field2"];
result[i] = newRecord;
}
return result;
}
Get(): Promise<{}> {
let expenses: Array<Model.ExpenseTransaction>;
let self = this;
return new Promise((resolve, reject) => {
$.ajax({
url: this.ExpenseTransaction,
type: "GET",
contentType: "application/json",
dataType: "json",
success: (data: any): void => {resolve(self.ConvertToEntity(data.value));},
error: (data: any) => { reject(data.status); }
});
});
};
Create(expense: Model.ExpenseTransaction): Promise<{}> {
return new Promise((resolve, reject) => {
$.ajax({
url: this.Connection,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(expense.toJSON()),
success: (data: any): void => {resolve(data.value);},
error: (data: any) => {reject(data.status);}
});
});
};
}
Compiled Typescript
define("CCSEQ/Model", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ExpenseTransaction extends ModelBase {
constructor() {
super();
}
toJSON() {
let json = {
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6506061600143a230c00090154250a010411044b070c0b01">[email protected]</a>": this.Connection + "(" + this.Field1 + ")",
"ccseq_Field2": this.Field2
};
return json;
}
}
exports.ExpenseTransaction = ExpenseTransaction;
define("CCSEQ/WebAPI", ["require", "exports", "CCSEQ/Model"], function (require, exports, Model) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ExpenseTransaction extends APIBase {
constructor() {
super();
this.ConvertToEntity = (data) => {
let result = new Array();
for (let i = 0; i < data.length; i++) {
let newRecord = new Model.ExpenseTransaction();
newRecord.Field1 = data[i]["_Field1_value"];
newRecord.Field2 = data[i]["Field2"];
result[i] = newRecord;
}
return result;
};
}
Get() {
let expenses;
let self = this;
return new Promise((resolve, reject) => {
$.ajax({
url: this.ExpenseTransaction,
type: "GET",
contentType: "application/json",
dataType: "json",
success: (data) => { resolve(self.ConvertToEntity(data.value)); },
error: (data) => { reject(data.status); }
});
});
}
;
Create(expense) {
return new Promise((resolve, reject) => {
$.ajax({
url: this.Connection,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(expense.toJSON()),
success: (data) => { resolve(data.value); },
error: (data) => { reject(data.status); }
});
});
}
;
}
exports.ExpenseTransaction = ExpenseTransaction;
JavaScript.js
requirejs.config({
bundles: {
'../CCSEQ.Library': ['CCSEQ/Model']
}
})
define(["require", "../jquery-3.1.1", "../papaparse.min", "CCSEQ/Model"], function (require, jquery, Papa, Model) {
"use strict";
$(document).ready(() => {
//$("#loading").hide();
setupHandlers();
});
function setupHandlers() {
"use strict";
$("#csv-file").change((e) => {
//$("#loading").show();
let file = e.target.files[0];
if (!file) {
return;
}
Papa.parse(file, {
complete: (results) => {
ImportExpenseTransaction(results.data);
}
});
});
}
function ImportExpenseTransaction(data) {
let importData = new Array();
data.forEach((expense) => {
let newRecord = new Library.Model.ExpenseTransaction();
newRecord.Field1 = expense["Field1"];
newRecord.Field2 = expense["Field1"];
importData.push(newRecord);
});
let expenseTransactionAPI = new ExpenseTransaction();
expenseTransactionAPI.Create(importData[0]).then(() => { console.log("success"); }).catch(() => { console.log("failure"); });
}
});