1

I have several functions nested under a initializePage function that gets executed once this runs:

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");

Is there a way to set a variable like this:

var dynamicTable = ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");

And not have it under some function because I need to keep it in the global scope. Returning it like this brings back a boolean value of false. The majority of the SharePoint app is under the initializePage function so I don't want to call it again in case it duplicates what was already on the page from the first time (it essentially makes a CSV file and HTML table from list data). I need this value to pass off to functions that will filter the table. Thanks in advance.

2 Answers 2

1

How about using window.dynamicTable anywhere you want, that sticks stuff in the global scope

It is of course better to create your own scope for variables/functions.

window.myScope = window.myScope || {}

or to make it MDS save

Type.registerNamespace('myScope');
0
0

This doesn't seem logically possible.

Think about it. ExecuteOrDelayUntilScriptLoaded does exactly what it says on the tin: if the specified script has already been loaded your function will be executed "immediately" but if the script is still not loaded then your function will wait in a queue until such process completes.
This means that you can't really know when the function will be executed in the first place, and as a consequence of this you can't know when the results of said function execution will be available too. Your function is going to be called asynchronously - you can't assign the return value because you don't have one yet.

To archive something similar to what you seem to need, you would probably need to either an "Observer" pattern (your function sect a variable you have defined somewhere - possibly in an app specific "namespace" and somewhere else an observer keeps monitoring such variable until a value become available) or a "Promise" based one (you define a promise for a value and that promise provides handlers that will get called either when the promise fulfills or gets broken - for example because an error occurred).

As it is now, your actual requirements/needs aren't very clear, so I can't really give a proof of concept example or suggest a specific solution. Fell free to add more detail and I will try to provide more insight.

2
  • Thank you for the really detailed answer. I knew there were limitations using it, but I wasn't sure how to go about passing this one variable to the next function, but also needing to delay executing initializePage since everything depends on that, and I couldn't do anything else with nixing ExecuteOrDelayUntilScriptLoaded. I guess specific timing of when it executes matters less, I just wanted to capture that variable whenever it did go off once instead of calling initializePage again. So it seems the function in ExecuteOr... should never return something?
    – LaLaLottie
    Commented Jun 22, 2016 at 20:20
  • @LaLaLottie As I said, you can't technically return anything from ExecuteOrDelayUntilScriptLoaded because the method is by nature asynchronous. To be able to return a value, ExecuteOrDelayUntilScriptLoaded would need to halt the script execution "Until Script Loaded & your function has been executed". That obviously doesn't mean that your function can't just place the value somewhere when ready - like Danny was suggesting in his answer: in this case, the observer patter I mentioned before can probably be helpful since you will need some logic to detect when the data is available. Commented Jun 23, 2016 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.