$ .ajax()ステートメントをes6 Promiseにキャストして、es6promiseを返そうとしています。アイデアは、Microsoft Dynamics Web APIへのCreate、Update、Delete呼び出しのアプリケーション層を作成し、es6 Promiseを返すことで、複数のページでCreate、Update、Delete呼び出しを再利用できるようにすることです。 Google 、 [〜#〜] mdn [〜#〜] 、および David Walsh Blog es6Promisesに関する記事を読みました。いくつかのSOの質問もありますが、私はまだ詳細を完全にまとめることができていません。
以下の私のコードでは、_ExpenseUpload.js
_がexpenseTransactionSetAPI.Create(newExpenseTransactionSet).then(...));
を呼び出すと、実行がthen()
に到達しているのがわかりますが、then()
内では何も実行されていません。コード実行が実際にthen()
を処理するためにどのような変更を加える必要があるのかよくわかりません。また、es6Promisesを正しく使用しているかどうかさえわかりません。任意のガイダンスをいただければ幸いです。
_"use strict";
requirejs.config({
bundles: {
'CCSEQ.WebAPI.js': ['Model/ExpenseTransaction', 'Model/ExpenseTransactionSet', 'API/ExpenseTransaction', 'API/ExpenseTransactionSet']
}
});
require(["Model/ExpenseTransaction", "Model/ExpenseTransactionSet", "API/ExpenseTransaction", "API/ExpenseTransactionSet"], function (ExpenseTransactionModel, ExpenseTransactionSetModel, ExpenseTransactionAPI, ExpenseTransactionSetAPI) {
let file;
$(document).ready(() => {
setupHandlers();
});
function setupHandlers() {
$("#csv-file").change((e) => {
file = e.target.files[0];
});
$("#btnUploadFile").click(() => loadFile());
}
function loadFile() {
Papa.parse(file, {
complete: (results) => {
ImportExpenseTransaction(results.data);
console.log("import complete");
}
});
}
function ImportExpenseTransaction(data) {
let newExpenseTransactionSet = new ExpenseTransactionSetModel.ExpenseTransactionSet();
newExpenseTransactionSet.SetName = $("#UploadName").val();
newExpenseTransactionSet.Month = $("#UploadMonth").val();
newExpenseTransactionSet.Year = $("#UploadYear").val();
newExpenseTransactionSet.ImportDate = new Date();
newExpenseTransactionSet.Status = 100000000;
let newExpenseTransactions = new Array();
data.forEach((expense) => {
if (expense[0] !== "PR EMP ID") {
let newRecord = new ExpenseTransactionModel.ExpenseTransaction();
newRecord. = expense[n];
... // Load other records like above
newExpenseTransactions.Push(newRecord);
}
});
let expenseTransactionSetAPI = new ExpenseTransactionSetAPI.ExpenseTransactionSet();
let expenseTransactionAPI = new ExpenseTransactionAPI.ExpenseTransaction();
expenseTransactionSetAPI.Create(newExpenseTransactionSet).
then((data) => {
console.log(data);
console.log("Transaction Set Created");
expenseTransactionAPI.
Create(newExpenseTransactions[0]).
then(() => {
console.log("Transaction Created");
}).catch(() => {
console.log("failure");
});
}).catch(() => {
(data) => {
console.log(data);
console.log("failure");
}
});
}
});
_
_define("API/ExpenseTransaction", ["require", "exports", "API/APIBase", "Model/ExpenseTransaction"], function (require, exports, APIBase_1, Model) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ExpenseTransaction extends APIBase_1.APIBase {
constructor() {
super();
this.ConvertToEntity = (data) => {
let result = new Array();
for (let i = 0; i < data.length; i++) {
let newRecord = new Model.ExpenseTransaction();
newRecord.[field] = data[i]["fieldName"];
.
.
.
result[i] = newRecord;
}
return result;
};
}
Create(expense) {
return new Promise((resolve, reject) => {
$.ajax({
url: this.ExpenseTransaction,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(expense.toJSON()),
success: (data) => { resolve(data); },
error: (data) => { reject(data); }
});
});
}
;
}
exports.ExpenseTransaction = ExpenseTransaction;
});
define("API/ExpenseTransactionSet", ["require", "exports", "API/APIBase", "Model/ExpenseTransactionSet"], function (require, exports, APIBase_2, Model) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ExpenseTransactionSet extends APIBase_2.APIBase {
constructor() {
super();
this.ConvertToEntity = (data) => {
let result = new Array();
for (let i = 0; i < data.length; i++) {
let newRecord = new Model.ExpenseTransactionSet();
newRecord.[field] = data[i]["fieldName"];
.
.
.
result[i] = newRecord;
}
return result;
};
}
Create(expenseSet) {
return new Promise((resolve, reject) => {
$.ajax({
url: this.ExpenseTransactionSet,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(expenseSet.toJSON()),
success: (data) => {
resolve(data);
},
error: (data) => {
reject(data);
}
});
});
}
;
}
exports.ExpenseTransactionSet = ExpenseTransactionSet;
});
//# sourceMappingURL=CCSEQ.WebAPI.js.map
_
ajax リクエストを返すだけで、promiseのようなオブジェクトが返されます。
JQuery1.5の時点で$ .ajax()によって返されるjqXHRオブジェクトは、Promiseインターフェイスを実装し、Promiseのすべてのプロパティ、メソッド、および動作を提供します。
Create(expense) {
return $.ajax({
url: this.ExpenseTransactionSet,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(expenseSet.toJSON())
});
}