http://mobile.tutsplus.com/tutorials/appcelerator/titanium-user-authentication/
I am following the same tutorial. This is my first time working on titanium mobile. i am using a mac and have added the same code given in the tutorial. But i am getting a Reference error: varialble not found. when call the 'fireEvent' function. Using mac i get the error and app crashes but on windows i dont get the error. What am i doing wrong in here.
3 Answers
Accepted Answer
hi Shreyash,
you can do that in other way also.. like passing your main window and tabGroup to your login window..
let me explain you more
in your app.js
var login = Titanium.UI.createWindow({ title:'User Authentication Demo', url:'authenticate/login.js' }); login.mainWin = main; login.tabGrp = tabGroup;in your login.js
var win = Ti.UI.currentWindow; var main = win.mainWin; var tabGroup = win.tabGrp;now you can access that two objects from your login.js window...
Arian thanks for responding. This is my app.js
// this sets the background color of the master UIView (when there are no windows/tab groups on it) Titanium.UI.setBackgroundColor('#fff'); var tabGroup = Titanium.UI.createTabGroup(); var main = Titanium.UI.createWindow(); var mainTab = Titanium.UI.createTab(); var login = Titanium.UI.createWindow({ title:'User Authentication Demo', url:'authenticate/login.js' }); var loginTab = Titanium.UI.createTab({ title:"Login", window:login }); var account = Titanium.UI.createWindow({ title:'Register', url:'authenticate/account.js' }); var accountTab = Titanium.UI.createTab({ title:'New Account', window:account }); tabGroup.addTab(loginTab); tabGroup.addTab(accountTab); tabGroup.open();And this is my login.js where i am trying to reference the var
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); } else { alert(response.message); } */ var json = this.responseText; var response = JSON.parse(json); if (response.logged == true) { username.blur(); password.blur(); Ti.App.fireEvent('loginSuccess', { name:response.name, email:response.email }); //win.close(); } else { alert(response.message); } }; Ti.App.addEventListener('loginSuccess', function(event) { main.tabBarHidden = true; main.title = 'Welcome ' + event.name; main.url = 'authenticate/main.js' main.name = event.name; main.email = event.email; mainTab.window = main; tabGroup.addTab(mainTab); tabGroup.removeTab(loginTab); tabGroup.removeTab(accountTab); });And this is what i get in my console
[WARN] Exception in event callback. { expressionBeginOffset = 2206; expressionCaretOffset = 2210; expressionEndOffset = 2210; line = 112; message = "Can't find variable: main"; name = ReferenceError; sourceId = 183426696; sourceURL = "file://localhost/Users/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/1F1C4868-7846-46C7-A986-48C6D31D0338/Login.app/authenticate/login.js"; }
When you create window with url property, this window will create a new JavaScript sub-context that will run in its own thread and global variable space.
In your case, code in login.js cannot access to main window in app.js. loginSuccess event listener should be moved to app.js - This's also mentioned in Step 3: Adding the ‘grantEntrance’ event
in above tutorial.
Add this to the bottom of your app.js:
Ti.App.addEventListener('loginSuccess', function(event) { main.tabBarHidden = true; main.title = 'Welcome ' + event.name; main.url = 'authenticate/main.js' main.name = event.name; main.email = event.email; mainTab.window = main; tabGroup.addTab(mainTab); tabGroup.removeTab(loginTab); tabGroup.removeTab(accountTab); });
Your Answer
Think you can help? Login to answer this question!