2011-06-24 70 views
1

我有多個按鈕在按鈕工具欄中加載我需要這些按鈕去不同的鏈接。但是,我一直無法弄清楚。以下是我試圖做的,但不起作用。如何鏈接Sencha觸摸工具欄中的按鈕?

 //Below are three buttons that need to go to three different links 
    var buttonsGroup1 = new Ext.Button({ 
    text: 'MESSAGES', 
    handler: tapHandler 
    }); 

    var buttonsGroup2 = new Ext.Button({ 
    text: 'HOLDS', 
    handler: tap2Handler 
    }); 

    var buttonsGroup3 = new Ext.Button({ 
    text: 'FINANCIALS', 
    handler: tapHandler 
    }); 

    //Click on the button and it goes to these links 
    var tapHandler = function (btn, evt) { 
     window.location = 'index.html'; 
    }; 

    var tap2Handler = function (btn, evt) { 
     window.location = 'redirect.php'; 
    }; 

    //Combine all the variables above and some other stuff and then add it to docked items very object oriented =) 
    var dockedItems = [ 
    { 
     xtype: 'toolbar', 
     title: 'Student Portal', 
     ui: 'light', 
     dock: 'top', 
     items: buttonsSpecTop, 
     defaults: { handler: tapHandler } 
    }, 
    { 
     xtype: 'toolbar', 
     ui: 'dark', 
     items: [buttonsGroup1, buttonsGroup2, buttonsGroup3], //This is where I load up all the buttons 
     dock: 'bottom', 
     layout:{ 
     pack: 'center' 
     }, 
     defaults: [handler: tapHandler, handler: tap2Handler] //This is the handler for the links 

    }]; 

回答

3

對於每個按鈕的配置,增加一個額外的配置PARAM說actionUrl這樣:

{ 
    xtype: 'toolbar', 
    actionUrl : 'http://www.google.com', 
    title: 'Student Portal', 
    ui: 'light', 
    dock: 'top', 
    items: buttonsSpecTop, 
    defaults: { handler: tapHandler } 
} 

現在,tapHandler功能,第一個參數是按鈕實例。所以,你可以得到的網址是這樣的:

function tapHandler(btn){ 
    console.log(btn); 
    window.open(btn.actionUrl); 
} 

而且你不能把在JavaScript的東西數組這樣裏面:

defaults: [handler: tapHandler, handler: tap2Handler] 

你必須使用一個對象,類似於第一之一:

defaults: { handler: tapHandler } 

而且,你不能把多個同名的參數有關的對象中,只有一個「處理」會去那裏。而對於默認值,只有一個處理程序將應用於所有項目。所以,如果你想要單獨的按鈕處理個別處理程序,請使用處理程序偵聽器來處理每個按鈕。

+0

我將如何使用處理程序偵聽器來查看每個按鈕 – Alex

+0

我可以看到,您已經將處理程序(tapHandler和tapHandler2)連接到每個單獨的按鈕。所以,只需刪除默認配置。它會工作。 – Swar