如何使用 jquery 或 javascript 在 api 函数调用之外 return 变量值
How to return variable value outside api function call using jquery or javascript
module.exports = function (ctx, application) {
var API;
var usermandData = [];
var clientmandData = [];
var taskmandData = []
API = {
GetMandatoryFieldsforUser: function () {
return MandatoryFields.User;
},
GetMandatoryFieldsforClient: function () {
return MandatoryFields.Client;
},
GetMandatoryFieldsforTask: function () {
return MandatoryFields.Task;
}
};
var UserFields = $.get("api/mapping/mandatoryfield?type=USER", function (d) {
this.mandatoryuserdata = JSON.parse(d.Data);
this.mandatoryuserdata.forEach(function (data) {
usermandData.push(data.DisplayName); // returns ["First Name", "Last Name", "Location", "Email"]
});
})
var ClientFields = $.get("api/mapping/mandatoryfield?type=CLIENT", function (d) {
this.mandatoryClientdata = JSON.parse(d.Data);
this.mandatoryClientdata.forEach(function (data) {
clientmandData.push(data.DisplayName); // returns ["Client Type", "Last Name / Entity Name", "Originating Location", "Responsible Person"],
});
})
var TaskFields = $.get("api/mapping/mandatoryfield?type=TASK", function (d) {
this.mandatoryTaskdata = JSON.parse(d.Data);
this.mandatoryTaskdata.forEach(function (data) {
taskmandData.push(data.DisplayName); //returns ["Client Name", "Period End Date", "Task Type", "Responsible Person"]
});
})
MandatoryFields = {
User: usermandData,
Client: clientmandData,
Task: taskmandData
}
Reqres.setHandler('Dataexportwizard:getMandatoryfields', function (type) {
// console.log(mandData)
switch (type) {
case "USER": return API.GetMandatoryFieldsforUser();
case "CLIENT": return API.GetMandatoryFieldsforClient();
case "TASK": return API.GetMandatoryFieldsforTask();
}
});
我的问题是因为它是一个异步函数 MandatoryFields.User
、MandatoryFields.Client
和 MandatoryFields.Task
returns null
值,我需要推送 usermandData
,clientmandData
,taskmandData
变成MandatoryFields.User
, MandatoryFields.Client
, MandatoryFields.Task
字段 如何实现它使用jquery 或 javascript.
由于 MandatoryFields 对象在 jQuery get 方法之前执行,我只能在函数调用中使用值,但我需要在 MandatoryFields 对象中使用它我如何使用 jquery
实现它
处理这些类型的异步任务的最佳解决方案之一是使用 "Promises"(ES6 添加的 JS 功能)。试试这段代码可能会有帮助:-
module.exports = function (ctx, application) {
var API;
var usermandData = [];
var clientmandData = [];
var taskmandData = []
API = {
GetStepContainerData: function () {
return StepContainerData;
},
GetStepData: function () {
return stepData;
},
GetMandatoryFieldsforUser: function () {
return MandatoryFields.User;
},
GetMandatoryFieldsforClient: function () {
return MandatoryFields.Client;
},
GetMandatoryFieldsforTask: function () {
return MandatoryFields.Task;
}
};
Promise.all([
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=USER", function (d) {
this.mandatoryuserdata = JSON.parse(d.Data);
this.mandatoryuserdata.forEach(function (data) {
usermandData.push(data.DisplayName); // returns ["First Name", "Last Name", "Location", "Email"]
});
resolve();
});
}),
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=CLIENT", function (d) {
this.mandatoryClientdata = JSON.parse(d.Data);
this.mandatoryClientdata.forEach(function (data) {
clientmandData.push(data.DisplayName); // returns ["Client Type", "Last Name / Entity Name", "Originating Location", "Responsible Person"],
});
resolve();
})
}),
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=TASK", function (d) {
this.mandatoryTaskdata = JSON.parse(d.Data);
this.mandatoryTaskdata.forEach(function (data) {
taskmandData.push(data.DisplayName); //returns ["Client Name", "Period End Date", "Task Type", "Responsible Person"]
});
resolve();
});
})
]
).then(function(result){
// After resolving all the promises this method will execute
MandatoryFields = {
User: usermandData,
Client: clientmandData,
Task: taskmandData
}
});
Reqres.setHandler('Dataexportwizard:getMandatoryfields', function (type) {
// console.log(mandData)
switch (type) {
case "USER": return API.GetMandatoryFieldsforUser();
case "CLIENT": return API.GetMandatoryFieldsforClient();
case "TASK": return API.GetMandatoryFieldsforTask();
}
});
module.exports = function (ctx, application) {
var API;
var usermandData = [];
var clientmandData = [];
var taskmandData = []
API = {
GetMandatoryFieldsforUser: function () {
return MandatoryFields.User;
},
GetMandatoryFieldsforClient: function () {
return MandatoryFields.Client;
},
GetMandatoryFieldsforTask: function () {
return MandatoryFields.Task;
}
};
var UserFields = $.get("api/mapping/mandatoryfield?type=USER", function (d) {
this.mandatoryuserdata = JSON.parse(d.Data);
this.mandatoryuserdata.forEach(function (data) {
usermandData.push(data.DisplayName); // returns ["First Name", "Last Name", "Location", "Email"]
});
})
var ClientFields = $.get("api/mapping/mandatoryfield?type=CLIENT", function (d) {
this.mandatoryClientdata = JSON.parse(d.Data);
this.mandatoryClientdata.forEach(function (data) {
clientmandData.push(data.DisplayName); // returns ["Client Type", "Last Name / Entity Name", "Originating Location", "Responsible Person"],
});
})
var TaskFields = $.get("api/mapping/mandatoryfield?type=TASK", function (d) {
this.mandatoryTaskdata = JSON.parse(d.Data);
this.mandatoryTaskdata.forEach(function (data) {
taskmandData.push(data.DisplayName); //returns ["Client Name", "Period End Date", "Task Type", "Responsible Person"]
});
})
MandatoryFields = {
User: usermandData,
Client: clientmandData,
Task: taskmandData
}
Reqres.setHandler('Dataexportwizard:getMandatoryfields', function (type) {
// console.log(mandData)
switch (type) {
case "USER": return API.GetMandatoryFieldsforUser();
case "CLIENT": return API.GetMandatoryFieldsforClient();
case "TASK": return API.GetMandatoryFieldsforTask();
}
});
我的问题是因为它是一个异步函数 MandatoryFields.User
、MandatoryFields.Client
和 MandatoryFields.Task
returns null
值,我需要推送 usermandData
,clientmandData
,taskmandData
变成MandatoryFields.User
, MandatoryFields.Client
, MandatoryFields.Task
字段 如何实现它使用jquery 或 javascript.
由于 MandatoryFields 对象在 jQuery get 方法之前执行,我只能在函数调用中使用值,但我需要在 MandatoryFields 对象中使用它我如何使用 jquery
实现它处理这些类型的异步任务的最佳解决方案之一是使用 "Promises"(ES6 添加的 JS 功能)。试试这段代码可能会有帮助:-
module.exports = function (ctx, application) {
var API;
var usermandData = [];
var clientmandData = [];
var taskmandData = []
API = {
GetStepContainerData: function () {
return StepContainerData;
},
GetStepData: function () {
return stepData;
},
GetMandatoryFieldsforUser: function () {
return MandatoryFields.User;
},
GetMandatoryFieldsforClient: function () {
return MandatoryFields.Client;
},
GetMandatoryFieldsforTask: function () {
return MandatoryFields.Task;
}
};
Promise.all([
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=USER", function (d) {
this.mandatoryuserdata = JSON.parse(d.Data);
this.mandatoryuserdata.forEach(function (data) {
usermandData.push(data.DisplayName); // returns ["First Name", "Last Name", "Location", "Email"]
});
resolve();
});
}),
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=CLIENT", function (d) {
this.mandatoryClientdata = JSON.parse(d.Data);
this.mandatoryClientdata.forEach(function (data) {
clientmandData.push(data.DisplayName); // returns ["Client Type", "Last Name / Entity Name", "Originating Location", "Responsible Person"],
});
resolve();
})
}),
new Promise(function(resolve,reject){
$.get("api/mapping/mandatoryfield?type=TASK", function (d) {
this.mandatoryTaskdata = JSON.parse(d.Data);
this.mandatoryTaskdata.forEach(function (data) {
taskmandData.push(data.DisplayName); //returns ["Client Name", "Period End Date", "Task Type", "Responsible Person"]
});
resolve();
});
})
]
).then(function(result){
// After resolving all the promises this method will execute
MandatoryFields = {
User: usermandData,
Client: clientmandData,
Task: taskmandData
}
});
Reqres.setHandler('Dataexportwizard:getMandatoryfields', function (type) {
// console.log(mandData)
switch (type) {
case "USER": return API.GetMandatoryFieldsforUser();
case "CLIENT": return API.GetMandatoryFieldsforClient();
case "TASK": return API.GetMandatoryFieldsforTask();
}
});