0

I have loginView object, that has 2 functions - initLogin and goAuth. Inside these methods I use keyword this. When using initLogin, I set value for this.loginField and for some other variables and everything is ok. But when I try to call goAuth, it says that this.loginField is "undefined parameter". Btw I do call initLogin before goAuth. Also, if I use loginView.pane - everything works properly.

So, why doesn't this work in goAuth, but does work in initLogin?

There's the code:

var loginView = {
pane: null,

hideButton: null,
exitButton: null,

loginField: null,
passwordField: null,
savePasswordBox: null,
enterButton: null,

registerButton: null,
forgotButton: null,

mask: null,

movePoint: null,

initLogin: function(){
    this.pane = loadFXML("dialog/login.fxml");
    LogHelper.info(this.pane==loginView.pane);
    this.pane.setOnMousePressed(function(event){ loginView.movePoint = new javafx.geometry.Point2D(event.getSceneX(), event.getSceneY())});
    this.pane.setOnMouseDragged(function(event) {
        if(loginView.movePoint === null) {
            return;
        }

        // Обновляем позицию панели
        stage.setX(event.getScreenX() - loginView.movePoint.getX());
        stage.setY(event.getScreenY() - loginView.movePoint.getY());
    });

    // Lookup hide & exit button
    this.hideButton = this.pane.lookup("#hidebtn").setOnAction(function(event){ stage.setIconified(true)});
    this.exitButton = this.pane.lookup("#exitbtn").setOnAction(function(event){ javafx.application.Platform.exit()});

    // Lookup login field
    this.loginField = this.pane.lookup("#logintf");
    this.loginField.setOnAction(this.goAuth);
    if (settings.login !== null) {
        this.loginField.setText(settings.login);
    }

    // Lookup password field
    this.passwordField = this.pane.lookup("#passwordtf");
    this.passwordField.setOnAction(this.goAuth);
    if (settings.rsaPassword !== null) {
        this.passwordField.setPromptText("*** Сохранённый ***");
    }

    // Lookup save password box
    this.savePasswordBox = this.pane.lookup("#rememberchb");
    this.savePasswordBox.setSelected(settings.login === null || settings.rsaPassword !== null);

    // Lookup action buttons
    this.enterButton = this.pane.lookup("#enterbtn").setOnAction(this.goAuth);
    this.registerButton = this.pane.lookup("#registerbtn").setOnAction(function(){openURL(config.registerURL)});
    this.forgotButton = this.pane.lookup("#forgotbtn").setOnAction(function(){openURL(config.forgotURL)});

    // Init overlays
    //processing.initOverlay();

    // Verify launcher & make request
    //verifyLauncher();
},

goAuth: function()
    // doesn't work, "loginField is undefined for this" 
    var login = this.loginField.getText();
    // does work
    //var login = loginView.loginField.getText();
    if (login.isEmpty()) {
        //TODO: some sort of exception
        return;
    }

    // Get password if online-mode
    var rsaPassword = null;
    if (!this.passwordField.isDisable()) {
        var password = this.passwordField.getText();
        if (!password.isEmpty()) {
            rsaPassword = settings.setPassword(password);
        } else if (settings.rsaPassword !== null) {
            rsaPassword = settings.rsaPassword;
        } else {
            return;
        }

        // Remember or reset password
        settings.rsaPassword = this.savePasswordBox.isSelected() ? rsaPassword : null;
    }

    // Show auth overlay
    settings.login = login;
    doAuth(login, rsaPassword);
}
};
chazsolo
  • 7,873
  • 1
  • 20
  • 44
  • @Ryan - i didn't find answer for my question there... – user3479037 Oct 24 '17 at 00:35
  • As stated in the answer to the linked question, the value of `this` for a regular function is determined by how it’s called. When you pass `this.goAuth` to `this.loginField.setOnAction`, you’re only passing the `goAuth` function – no `this`. You can create a new function that wraps `this.goAuth` and always passes the same value of `this` along with `this.loginField.setOnAction(this.goAuth.bind(this))`. – Ry- Oct 24 '17 at 00:55

0 Answers0