无法限制Ti.App.fireEvent多次执行

时间:2016-02-19 18:52:35

标签: ios appcelerator appcelerator-titanium appcelerator-alloy

在我的应用程序中,Ti.App.addEventListener执行了多次甚至fireEvent调用一次 我无法删除fireEvent。 如何删除创建的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();
        });

    });

1 个答案:

答案 0 :(得分: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);
    [...]
}

- 编辑(答案评论)

这是因为您每次调用EventListener时都会创建全局Ti.App.addEventListener selectUser

一种可能的选择是将event侦听器绑定到userList创建,如:

[...]
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();
[...]