javascriptoffice-2013

Office 2013 Javascript API - Pass Variable to Callback of ".BindingDataChanged"


Background

I am writing a sidebar app for Excel 2013, and I have a wrapper function which updates the view of the sidebar. I need to trigger that function (and so trigger an update of the view) from multiple events, one of which is when the data in a bound area changes.

The problem I'm having is that I need to pass along a variable to that wrapper function. It needs data which I want to be able to save in a Setting and then load just once.


Code

Current Code:

Office.select("bindings#"+bindingID).addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged);

function onBindingDataChanged(eventArgs) {
    searchThroughData(eventArgs.binding.id);
}
function searchThroughData(bindingID) {
    //repaint view
}

The above works to trigger the repaint. But it doesn't include the passed variable. What I'd expect is for the code to be something like this:

Attempted, Doesn't Work:

Office.select("bindings#"+bindingID).addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged(eventArgs,data));

function onBindingDataChanged(eventArgs,data) {
    searchThroughData(eventArgs.binding.id,data);
}
function searchThroughData(bindingID,data) {
    //repaint view
}

This doesn't work, however.


Question

Any ideas how I can pass along this variable?


Solution

  • In your attempted solution, you are calling the onBindingDataChanged function instead of making it available for the handler to call. You'd need to do something like this (assuming that the variable "data" is available beforehand)

    Office.select("bindings#"+bindingID).addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged(data));
    
    function onBindingDataChanged(data) {
        return function(eventArgs) {
            searchThroughData(eventArgs.binding.id, data);
        };
    }
    
    function searchThroughData(bindingID,data) {
        //repaint view
    }
    

    If data is a global variable, you can just do

    Office.select("bindings#"+bindingID).addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged(data));
    
    function onBindingDataChanged(eventArgs) {
        searchThroughData(eventArgs.binding.id, data);
    }
    
    function searchThroughData(bindingID,data) {
        //repaint view
    }