none
setDataAsync scoping issue RRS feed

  • Question

  • I am trying to do the following:

    function BindingSetDataAsync(bindingName, data, options) {

    var result; Office.select("bindings#" + bindingName).setDataAsync(data, options, function (setDataAsyncResult) { result = setDataAsyncResult.status; }); return result; }

    But from what I understand the "result" variable in the lamda function callback is a copy of the one in my "BindingSetDataAsync" function, not by reference, so that one is not set, hence the function return is always "undefined".

    From the many versions of this function I tried it seems to boil down to a scoping issue with how to link "setDataAsync" to my function. Using "asyncContext" to pass in the variable pointer allows access to the value in the callback, but doesn't pass back that value either...

    Any help will be appreciated!

    Tuesday, April 1, 2014 2:27 PM

Answers

  • Hi,

    This is not a scope issue. Because the method setDataAsyncResult is asynchronous, so the code ‘return result’ may execute before or after the setDataAsyncResult method. If the code execute after setDataAsyncResult you will get the correct result, otherwise you get undefined error.

    I suggest you creating a div element and save the message via it. You can refer to code below( Refer to Binding.setDataAsync method):

    // Function that writes to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 2, 2014 6:44 AM
    Moderator

All replies

  • Hi,

    This is not a scope issue. Because the method setDataAsyncResult is asynchronous, so the code ‘return result’ may execute before or after the setDataAsyncResult method. If the code execute after setDataAsyncResult you will get the correct result, otherwise you get undefined error.

    I suggest you creating a div element and save the message via it. You can refer to code below( Refer to Binding.setDataAsync method):

    // Function that writes to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 2, 2014 6:44 AM
    Moderator
  • Thanks for the reply, I managed to get a wrapper function using success/fail callback parameters to work instead of the true/false I envisioned.

    Of note is that I did try adding a JQuery wait after the function call to give the Async time to set the variable before I use it, but this did not work.

    function SelectSetDataAsync(binding_name, data, options, fail_callback, succeed_callback) {
    	Office.select("bindings#" + binding_name, AsyncError).setDataAsync(data, options, function (set_data_async_result) {
    
    		AsyncError(set_data_async_result);
    
    		if (set_data_async_result.status === Office.AsyncResultStatus.Succeeded) {
    			succeed_callback();
    		} else {
    			fail_callback();
    		}
    	});
    }
    

    Thursday, April 3, 2014 2:11 PM