How to pass argument to callback and don't lose bind?

49 views Asked by At

I have the following code:

$.getJSON('getAllTerminals.json', renderTerminalsOnMapAndFitBounds.bind({index:globalRequestCounter++, navigateToTypedText:true}))

...
function renderTerminalsOnMapAndFitBounds(data, updateSelectedTerminals) {
        renderTerminalsOnMap.call(this,data);
        fitBounds();
        if(this.navigateToTypedText === true){
            navigateMapToTypedAdress();
        }
        if (updateSelectedTerminals) {
            $.getJSON('getSelectedTerminals', {}, function (json) {
                window.MARC.addTerminalPage.terminalsSelected = json;
                update();
                initPage();
            });
        }
    }

Can you advise me how to make that all works as now but to renderTerminalsOnMapAndFitBounds was passed updateSelectedTerminals as true ?

1

There are 1 answers

3
Bergi On BEST ANSWER

No, you cannot use bind to partially apply non-initial parameters (and there's no flip). Just use a function expression:

$.getJSON('getAllTerminals.json', function(data) {
    renderTerminalsOnMapAndFitBounds.call({
        index:globalRequestCounter++,
        navigateToTypedText:true
    }, data, true);
});

If you have to use bind, either change the parameter order of renderTerminalsOnMapAndFitBounds, or make it accept that updateSelectedTerminals parameter as a property of the this object.