简体   繁体   English

如何将参数传递给回调并且不丢失绑定?

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

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 ? 您能建议我如何使所有内容像现在一样工作,但是renderTerminalsOnMapAndFitBounds传递为true的updateSelectedTerminals吗?

No, you cannot use bind to partially apply non-initial parameters (and there's no flip ). 不,您不能使用bind来部分地应用非初始参数(并且没有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. 如果必须使用bind,请更改renderTerminalsOnMapAndFitBounds的参数顺序,或者使其接受该updateSelectedTerminals参数作为this对象的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM