0

在我的應用中,Ti.App.addEventListener多次執行多次fireEvent調用一次。
我無法刪除fireEvent。 如何刪除創建的fireEvent。無法限制Ti.App.fireEvent多次執行

這裏是我的示例文件:

selectUser.xml

Alloy> 
    <NavigationWindow id="selectUserNav" platform="ios"> 
     <Window id="selectUserWin"> 
      <Label id="selectUserLabel"/> 
      <Label id="userName"/> 
     </Window> 
    </NavigationWindow> 
</Alloy> 

selectUser.js

var selectedUser = ''; 
function displaySelectedUser(){ 
    $.selectUserLabel.setText('Select the user'); 
    $.userName.setText(selectedUser); 
} 
displaySelectedUser(); 

$.selectUserWin.addEventListener('open', function() { 
    var titleLabel = Ti.UI.createLabel({ 
     text : 'Select User', 

    }); 
    $.selectUserWin.setTitleControl(titleLabel); 
    var leftView = Ti.UI.createView({ 
    }); 
    var leftButton = Titanium.UI.createButton({ 
     backgroundImage : '/left_arrow.png' 
    }); 
    leftView.add(leftButton); 
    $.selectUserWin.setLeftNavButton(leftView); 
    var rightView = Ti.UI.createView({ 
    }); 
    var rightButton = Titanium.UI.createButton({ 
     backgroundImage : '/right_arrow.png' 
    }); 
    rightView.add(rightButton); 
    $.selectUserWin.setRightNavButton(rightView); 

    leftButton.addEventListener('click', function(e) { 
     $.selectUserNav.close(); 
    }); 
    rightButton.addEventListener('click', function(e) { 

      Alloy.createController('usersList').getView().open(); 
    }); 
}); 

Ti.App.addEventListener('username', function(data) { 

    Ti.API.info('### selectedUser: '+ data.selectedUserName); 
    selectedUser = data.selectedUserName; 
    displaySelectedUser(); 

}); 

usersList.xml

Alloy> 
    <NavigationWindow id="usersListNav" platform="ios"> 
     <Window id="usersListWin"> 
      <TableView id="usersDispTable"></TableView> 
     </Window> 
    </NavigationWindow> 
</Alloy> 

usersList.js

var usersTableData = []; 
    function userList(){ 
     for (i = 0; i < 5; i++) { 

       var titleLabel1 = Titanium.UI.createLabel({ 
        text : 'srinivas', 
       }); 
       var userListRow = Ti.UI.createTableViewRow({ 
         userName : 'srinivas', 
         height : '40', 
       }); 
       userListRow.add(titleLabel1); 
       usersTableData.push(userListRow); 

       userListRow.addEventListener('click', function(e) { 
        var selectedArgs = { 
         selectedUserName : e.rowData.userName 
        }; 
        Ti.App.fireEvent('username', selectedArgs); 
        $.usersListNav.close(); 
       }); 

     } 
$.usersDispTable.setData(usersTableData); 

    } 
    userList(); 
    $.usersListWin.addEventListener('open', function() { 
     var titleLabel = Ti.UI.createLabel({ 
      text : 'Users List', 

     }); 
     $.usersListWin.setTitleControl(titleLabel); 
     var leftView = Ti.UI.createView({ 
     }); 
     var leftButton = Titanium.UI.createButton({ 
      backgroundImage : '/left_arrow.png' 
     }); 
     leftView.add(leftButton); 
     $.usersListWin.setLeftNavButton(leftView); 

     leftButton.addEventListener('click', function(e) { 
      $.usersListNav.close(); 
     }); 

    }); 

回答

2

抱歉,但我會間接地回答你的問題,因爲你正在嘗試做的是效率不高。

你需要做的就是聽你的TableView不是你TableViewRow像下面的例子:

$.usersDispTable.addEventListener('click',function(e){ 
    Ti.App.fireEvent('username', e.rowData.username); 
} 

順便說一句,如果你需要刪除一個EventLister你可以這樣做:

window.addEventListener('click', function foo(e) { 
    window.removeEventListener('click', foo); 
    [...] 
} 

- 編輯(回答評論)

那是因爲您正在創建一個全球EventListenerTi.App.addEventListener每次調用selectUser

一個可能的選項是將event監聽綁定到用戶列表的創建,如:

[...] 
var userList = Alloy.createController('usersList').getView(); 
userList.addEventListener('username',function setUsername(username){ 
    userList.removeEventListener('username',setUsername); 
    Ti.API.info('### selectedUser: '+ data.selectedUserName); 
    selectedUser = data.selectedUserName; 
    displaySelectedUser(); 
}); 
userList.open(); 
[...] 
+0

其實我想通過從usersList.js控制器所選擇的用戶名之前打開屏幕即selectUser.js 當我打開兩個控制器多次調用selectUser.js Ti.App.addEventListener多次調用。 – Chanakya

+0

fireEvent在usersList.js: userListRow.addEventListener( '點擊',函數(E){ VAR selectedArgs = { selectedUserName:e.rowData.userName }; Ti.App.fireEvent( '用戶名',selectedArgs ); $ .usersListNav.close(); }); – Chanakya

+0

EventLister在selectUser.js:(下面聽者多次調用) Ti.App.addEventListener( '用戶名',功能(數據){ Ti.API.info( '### selectedUser:' + data.selectedUserName ); selectedUser = data.selectedUserName; displaySelectedUser(); }); – Chanakya