return doesnot work with pagemethods - javascript

I wonder how to return a value using page methods. the following code gives me error
function main()
{
PageMethods.custref(ddlpf.options[ddlpf.selectedIndex].value,custref1.value,custSuc,custErr);
function custSuc(boo)
{
if(boo==true)
{message("Cust ref already exists");btn_enable(false);make_null();return;}
}
function custErr(){}
Pagemethods.set("true",suc,err);
function suc(res){//code}
function err(){}
}
my problem is even the message displayed, "set" pagemethod is working

change it as the following
function main()
{
PageMethods.custref(ddlpf.options[ddlpf.selectedIndex].value,custref1.value,custSuc,custErr);
function custSuc(boo)
{
if(boo==true)
{message("Cust ref already exists");btn_enable(false);make_null();return;}
Pagemethods.set("true",suc,err);
function suc(res){//code}
function err(){}
}
function custErr(){}
}

Related

How to reuse conditional check for each function?

I have many functions example like this
function update() {
if (isAdminUser()) {
return false;
}
...
}
function get() {
if (isAdminUser()) {
return false;
}
...
}
...
is there any possible way to have the conditional statement
if (isAdminUser()) {
return false;
})
written once and run by itself at the beginning of each function. I'm using javascript
You could use a higher order function to encapsulate the logic needed to run before a specific function is run. Higher order functions take functions as parameters, therefore a possible solution to your problem could look like this:
function withIsAdminUser(callback) {
return function() {
if (isAdminUser()) {
return false;
}
return callback();
}
}
function getRaw() {
// Do something here, this whole function could also be inlined
}
const get = withIsAdminUser(getRaw);
If you use TypeScript try decorators.
Docs: https://www.typescriptlang.org/docs/handbook/decorators.html#decorators
maybe you can define a function that can accept another function as an argument and returns false if that function (i.e isAdminUser in your code snippet) returns true
const checkUser = func => func() && false
then the function can be used like:
function update() {
if (checkUser(isAdminUser)) {
// update() logic will only run if user is not admin
}
}
function get() {
if (checkUser(isAdminUser)) {
// get() logic will only run if user is not admin
}
}

html inject call function with parameter

I have a problem where if i want to add a parameter to my click attribute then it calls the function as soon as it renders
here is my test html:
return html`
<button class="menu-btn" #click="${this._OpenSubMenu(1)}>test</button>"
`;
}
And the function:
_OpenSubMenu(test:number) {
console.log("Hello")
}
This output Hello as soon as the page is rendered.
So how can i avoid this while still adding a parameter to my function?
You need to make your function return a function. Your click function will then execute the returned function, and due to closure's will still have access to the params.
eg..
_OpenSubMenu(test:number) {
var that = this;
return function () {
console.log("Hello");
//test is also a closure so you can use here
//that will equal this
}
}
If you want access to this, you could also use an arrow function
_OpenSubMenu(test:number) {
return () => {
console.log("Hello");
//test is also a closure so you can use here
//this will also still be valid here
}
}

Wrap body.getAsync() inside a sync function

So I was following a tutorial to build an Outlook Add-in. However, the demo does not display the body of the message.
I also learned from the doc that I can call the getAsync to access to the body but it does not work. Do I need to use async await here?
Here is the code:
function loadProps() {
$("#attachments").html(buildAttachmentsString(item.attachments));
$("#cc").html(buildEmailAddressesString(item.cc));
$("#conversationId").text(item.conversationId);
$("#from").html(buildEmailAddressString(item.from));
$("#internetMessageId").text(item.internetMessageId);
$("#normalizedSubject").text(item.normalizedSubject);
$("#sender").html(buildEmailAddressString(item.sender));
$("#subject").text(item.subject);
$("#to").html(buildEmailAddressesString(item.to));
$("#body").text(buildEmailBodyString()); //async function
}
function buildEmailBodyString() {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
return resText.value;
});
}
Your issue is that your buildEmailBodyString fires off getAsync and exists immediately. It isn't returning restText.value from the function because the function already existed.
function buildEmailBodyString() {
// 1. Fires function
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
// 3. returns a value to nothing
return resText.value;
});
// 2. Exits function
}
One solution here would be to set $("#body") from within the callback:
function buildEmailBodyString() {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
$("#body").text(resText.value);
});
}
You could also drop buildEmailBodyString entirely and call it within loadProps directory. This would simplify the code so it's a bit easier grok down the road:
function loadProps() {
$("#attachments").html(buildAttachmentsString(item.attachments));
$("#cc").html(buildEmailAddressesString(item.cc));
$("#conversationId").text(item.conversationId);
$("#from").html(buildEmailAddressString(item.from));
$("#internetMessageId").text(item.internetMessageId);
$("#normalizedSubject").text(item.normalizedSubject);
$("#sender").html(buildEmailAddressString(item.sender));
$("#subject").text(item.subject);
$("#to").html(buildEmailAddressesString(item.to));
// Retrieve Email Body
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
$("#body").text(resText.value);
});
}

How to get last executed function name using Javascript

I am trying to get the name of the last executed function using JavaScript and this following code is working for me but need to find any other method is available? please help me:
var fname;
function fn()
{
fname="fn()";
}
function fn1()
{
fname="fn1()";
}
function fexecute()
{
setTimeout(fname, 0);
}
I want to get finally executed function name and when i call fexecute() function at that time call last executed function.
If you want to execute a function name that you have in a string, you will have to do some besides just pass the string to setTimeout(). The best option would be to save a reference to the function directly rather than a string name.
var lastFunc;
function fn()
{
lastFunc=fn;
}
function fn1()
{
lastFunc=fn1;
}
function fexecute()
{
// execute previous function after a short timer
setTimeout(lastFunc, 0);
}
If you don't need the setTimeout(), then you can execute the last function like this:
function fexecute()
{
// execute previous function
lastFunc();
}
Just use arguments.callee.name
var fname;
function anyFunction () {
fname = arguments.callee.name;
}
function anyOtherFunction () {
fname = arguments.callee.name;
}
Call the functions normally like anyFunction(); and anyOtherFunction();

Immediate object initialization confusion

I am working on a js file that makes use of JScroll. The callback for the jsp-scroll-y event is defined in the following function
function initWall() {
//callback from jqueryscrollpane
Scroll_TimeLine_Instance = function (event, scrollPositionY, isAtTop, isAtBottom){
//get more content
if (isAtBottom) {
GetMoreDate(objid, vwrsid, secid, orgid, incflwr, qty, mintlid, maxtlid, successGetTimeLineCallback, failureGetTimeLineCallback);
}
}();
}
Another function is defined that then binds this callback to the jsScroll
function reapplyScroll() {
Utilities.DestroyScrollBar($(_target).closest('.widgetBody'));
Utilities.ApplyScrollBar($(_target).closest('.widgetBody'), false, Scroll_TimeLine_Instance);
}
Utilities.ApplyScrollBar = function (element, showScrollBar, scrollCallback) {
$(element).jScrollPane({
horizontalGutter: 5,
verticalGutter: 5,
'showArrows': false
}).bind('jsp-scroll-y', scrollCallback);
if (!showScrollBar) {
$(element).find('.jspDrag').hide();
}
}
The callback was never called, and I found this was because it was undefined. If I remove the Immediate object initialization (); from after the creation of the function everything works fine.
Can anyone explain this? I don't understand why it was being called immediate anyway, so i assume this is an error on the part of whoever created it, and I have no idea why it would cause this variable to be undefined?
It is undefined because the function (that is immediately called) does not return any value
So it seems indeed that this is a bug of the library..
Either remove the (); at the end, or if you want to call it right there as well just invoke it in the following line
function initWall() {
//callback from jqueryscrollpane
Scroll_TimeLine_Instance = function (event, scrollPositionY, isAtTop, isAtBottom){
//get more content
if (isAtBottom) {
GetMoreDate(objid, vwrsid, secid, orgid, incflwr, qty, mintlid, maxtlid, successGetTimeLineCallback, failureGetTimeLineCallback);
}
}; /// this is assigning the function to our variable
Scroll_TimeLine_Instance (); // this is the invokation
}

Categories