Java script extension "disapear" after switch tab

Hi,
I have below Java Script Extension code added to init() (I also tried adding to postinit()) of an editor class:

com.vaadin.ui.Button btn = exportButton.unwrap(com.vaadin.ui.Button.class);
btn.setId(exportButton.getId() + System.currentTimeMillis());
new IOSViewPDF(btn, iospdfUrl);

And below IOSViewPDF code:

@JavaScript("vaadin://iosviewpdf-extension.js")
public class IOSViewPDF extends AbstractJavaScriptExtension {
    public IOSViewPDF(Button button,String url){
        super(button);
        callFunction("init",url,button.getId());
    } 
}

And with below JS code:

window.cn_com_dataocean_cip_web_guiviewpdf_extension_IOSViewPDF = function () {
    var self = this;
    self.init = function (url,id) {
        $("#"+id).click(function(){
            IOSViewTest(url);
        });
    }
}

It works fine - while I click the button, it calls native method IOSViewTest(url).

But while I switch to other tab and switch back, the event (click) add to the button disapears.

I have cuba.web.mainTabSheetMode set to DEFAULT, after set it to MANAGED, the event (click) stay there after switch back.

But I don’t want to change cuba.web.mainTabSheetMode for now, is there any other way to solve this?

Hello @anjingjing

The problem is that the JS init method is invoked only once. You should move your subscription to upper level:

window.cn_com_dataocean_cip_web_guiviewpdf_extension_IOSViewPDF = function () {
    var self = this;
    var button = self.getElement(self.getParentId());

    $(button).click(function () {
        IOSViewTest(url);
    });
}

Regards,
Daniil

1 Like