如何在钛金属移动设备上关闭其他JS文件的模态窗口?

时间:2012-10-12 09:30:25

标签: javascript titanium titanium-mobile

我在钛金属移动设备中创建这个简单的登录应用程序..这是我的代码:

Login.js

function Login() {
    var loginView = Titanium.UI.createView({
        backgroundColor:'#C4FBFF',
        layout:'vertical'
    });

    var txtUsername = Titanium.UI.createTextField({
        width:'75%',
        hintText:'Username',
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
    });

    var txtPassword = Titanium.UI.createTextField({
        width:'75%',
        hintText:'Password',
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
        passwordMask:true
    });

    var btnLogin = Titanium.UI.createButton({
        title:'Login',
        width:'75%'
    });

    loginView.add(txtUsername);
    loginView.add(txtPassword);
    loginView.add(btnLogin);

    btnLogin.addEventListener('click',function(e){
        var alertDialog = Titanium.UI.createAlertDialog({ 
            title: 'Confirmation', 
            message: 'You will be logged in as ' + txtUsername.value + ', continue?',
            buttonNames: ['Yes','No'] 
        });

        alertDialog.addEventListener('click',function(e){
            if (e.index === 0){ //Yes Pressed
                var MainMenu = require('ui/common/MainMenu');

                var mainMenuWindow = Titanium.UI.createWindow({
                    backgroundColor:'#336699',
                    title:'Main Menu',
                    modal:true
                });

                var mainMenu = new MainMenu();
                mainMenuWindow.add(mainMenu);

                mainMenuWindow.open();
            }
            else{ // No Pressed
                makeAlert('Login','Please contact your system administrator');
            }
        });

        alertDialog.show();
    });

    function makeAlert(title, message) {
        var customAlertDialog = Titanium.UI.createAlertDialog({ 
            title: title, 
            message: message,
            buttonNames: ['Ok']
        });
        return customAlertDialog.show();
    }

    return loginView;
}

module.exports = Login;

这是我的MainMenu.js:

function MainMenu(){
    var mainMenuView = Titanium.UI.createView({
        backgroundColor:'#C4FBFF',
        layout:'vertical'
    });

    var btnClose = Titanium.UI.createButton({
        title:'Close',
        height:30,
        width:150
    });

    mainMenuView.add(btnClose);

    btnClose.addEventListener('click',function(e){
    //What should i do here?
    }); 
    return mainMenuView;
}    
module.exports = MainMenu;

主要问题是我想在按下关闭按钮时关闭模态窗口。我已经尝试了Titanium.UI.currentWindow.close()但它关闭了我的所有应用程序,并且它无法在iOS设备上执行。任何建议?非常感谢..

1 个答案:

答案 0 :(得分:3)

// pass parent window in as parameter
var mainMenu = new MainMenu(mainMenuWindow);
mainMenuWindow.add(mainMenu);

现在使用参数

function MainMenu(_parent /*parent window*/){

    // other code ...

    btnClose.addEventListener('click',function(e){
        _parent.close();
    });

}