pol_guy
pol_guy

Reputation: 477

Titanium Close Window and Open New One

I am building an iOS application in Titanium. The first window is a login page. When the user inputs their username and password, those values are sent to a PHP file for authentication.

If the user is authenticated - i.e. they have a unique username/password combo, I want the current window to close and the new window to open.

The values are being sent to the PHP file and the user is being authenticated; however, when the code that close the current window runs (Titanium.UI.currentWindow.close), throws an error indicating that file that opens the new window doesn't exist. The file referencing the new window does exist though.

I have moved the code that appears to be causing the error into many places but with the same result.

var loginReq = Titanium.Network.createHTTPClient();

loginReq.onload = function()
{
    var json = this.responseText;
    var response = JSON.parse(json);
    if (response.logged == true)
    {
        alert("Welcome " + response.name + ". Your email is: " + response.email);
        username.value = '';
        password.value = '';
        //This creates the new window after user authentication.
        var menuPage = Titanium.UI.createWindow({
        title: "Menu",
        tabBarHidden: false,
        url: 'menuPage.js'
        });
        //This is supposed to close the current window.
        Titanium.UI.currentWindow.close();
        //This is supposed to open the new window.
        menuPage.open();
    }
    else
    {
        alert(response.message);
    }
};

Upvotes: 1

Views: 1662

Answers (3)

Đoàn Nguyễn
Đoàn Nguyễn

Reputation: 27

If you using Alloy:

file windowlogin.xml

<Alloy>
	<Window id="window_login">
		<TextField id="txt_username"></TextField>
		<TextField id="txt_password"></TextField>
		<Button title="Login" id="btn_login"></Button>
	</Window>
</Alloy>

file windowlogin.js

$.btn_login.addEventListener("click", function(e){
	var loginUrl = "http://domain.com/login";
	var dataLogin = {
		username: $.txt_username.value,
		password: $.txt_password.value
	};
	var loginReq = Titanium.Network.createHTTPClient();

	loginReq.onload = function()
	{
	    var json = this.responseText;
	    var response = JSON.parse(json);
	    if (response.logged == true)
	    {
	        alert("Welcome " + response.name + ". Your email is: " + response.email);
	        username.value = '';
	        password.value = '';
	        //This creates the new window after user authentication.
	        var menuPage = Titanium.UI.createWindow({
			title: "Menu",
			tabBarHidden: false,
			url: 'menuPage.js'
			});
			//This is supposed to open the new window.
	        menuPage.open();
	        //This is supposed to close the current window.
	        $.window_login.close();
	    }
	    else
	    {
	        alert(response.message);
	    }
	};
	
	loginReq.open("POST", loginUrl);
	loginReq.send(dataLogin);
});

Upvotes: 0

Rene Pot
Rene Pot

Reputation: 24815

Instead of closing the window first, and then opening the new one, you should first open the new window, and once opened, close the old one from the new window.

menuPage.open({closeWindow: function(){ 
    curWin.close(); }
});

Then, in menuPage.js, watch for the the open event, and once that is fired, call the function you passed to menuPage.js.

I would however recommend diving into Alloy. This method is very outdated.

In Alloy you would do it like this:

Alloy.createController('menuPage', {closeWindow: function(){ 
    $.getView().close();
});

In menupage:

$.getView().addEventListener('open',args.closeWindow());

Upvotes: 2

PRASHAANTH
PRASHAANTH

Reputation: 50

Hi try this to open a window

var menuPage = require('menuPage');
win = new menuPage({title:''});

Thanks

Upvotes: 0

Related Questions