2012-08-12 29 views
1

我有一個導航組用於登錄。如果用戶登錄成功,那麼應用程序關閉導航組(具有navGroup.close,我認爲這會被銷燬)並創建一個選項卡組。當用戶註銷標籤組關閉並且導航組被重新創建時。問題是facebook登錄按鈕(它在導航組上)是第一次創建導航組,但第二次執行「登錄」監聽器2次和3次時間等等。 下面是一個簡化的代碼,生成此錯誤:Titanium問題與Facebook登錄按鈕和導航組

app.js

Ti.App.Properties.setString("fbAccess","login"); 

Ti.Facebook.appid = 'xxxxxxxxxx'; 
Ti.Facebook.permissions = ['publish_stream','offline_access','email']; 
//Ti.Facebook.forceDialogAuth = false; 
Ti.include('preLogin.js'); 

loginFBC.js

Ti.Facebook.addEventListener('login', function(e) { 

    Ti.API.info("inside login"); 

    if ((e.success) && (Ti.App.Properties.getString("fbAccess")=="login")) { 
         Ti.API.info("inside if"); 

        navGroup.close(); 
        Ti.include('preLogin.js'); 
         }}); 

preLogin.js

var buttonLoginFB = Titanium.UI.createButton({ 
    top: "90%", 
    width: "70%", 
    height: "12%"}); 

var preLoginWin = Titanium.UI.createWindow({ 
    titleImage:'/icons/logoForBar.png',  
    title:"Back", 
    backgroundColor:'#fff', 
    barColor:'00aeef', 
    }); 
var buttonLogin = Titanium.UI.createButton({ 
    title: 'Login', 
    top: "30%", 
    width: "70%", 
    height: "12%", 
    opacity:1 
    }); 

var buttonRegister = Titanium.UI.createButton({ 
    title: 'Register', 
    top: "60%", 
    width: "70%", 
    height: "12%", 
    opacity:1 

    }); 




    var navGroup = Ti.UI.iPhone.createNavigationGroup({ 
        window:preLoginWin 
    }); 

    preLoginWin.add(buttonLogin,buttonRegister,buttonLoginFB); 

    var main = Ti.UI.createWindow(); 

    main.add(navGroup); 
    main.open(); 

Ti.include('/loginFBC.js'); 
//include the preLoginC.js that holds the code for the buttons listeners 
Ti.include('/preLoginC.js'); 

和preLoginC.js

buttonLoginFB.addEventListener("click", function(e) { 

Ti.API.info("button"); 

Ti.Facebook.logout(); 
Ti.Facebook.appid = 'xxxxxxxxxxx'; 
Ti.Facebook.permissions = ['publish_stream','offline_access','email']; 
//Ti.Facebook.forceDialogAuth = false; 
Ti.Facebook.authorize(); 


}); 
+0

你找到解決這個問題的方法 – Dev 2014-03-20 10:53:24

回答

0

你真的應該重新考慮你如何佈置您的應用程序,首先,你定義了一切都在全球JavaScript的命名空間,這是一個非常糟糕的主意:請參閱this talk on best practices for Javascript with Titanium.

你應該第二件事不是做的是有一個導航組與標籤欄。相反,爲什麼不直接用這種一次性模式打開一個新窗口呢?

var modalWindow = Ti.UI.createWindow({...formatting here...}); 
modalWindow.open({modal : true}); 

壞的做法,不過您看多的Facebook窗口彈出是因爲你已經添加了多個事件偵聽器來 buttonLoginFB按鈕的原因。發生這種情況的原因包括: preLogin.js當您授權Facebook時,其中包括 preLoginC.js這是添加您的偵聽器。

要解決此問題,您需要根據Titanium最佳做法更改您的一般體系結構。將UI元素移動到更安全的名稱空間中,並且不在事件偵聽器中包含Javascript文件,這將始終導致問題。

希望這會有所幫助!