andrerpena / chatjs

Platform-independent jQuery plugin for chatting
MIT License
184 stars 91 forks source link

Fill userlist from mysql Database #24

Open ehrenberg opened 8 years ago

ehrenberg commented 8 years ago

Hi everyone,

I've tried to fill my Userlist with Ajax in my own Adapter. But no users shown. The Array is filled. I'am confused. Maybe some help? Thanks alot!

this.users = new Array();
        $users = this.users;
        $.ajax({
            url : "/plugins/messages/ajax.php",
            type : "POST",
            dataType: 'json',
            data : {
                type : "get_userlist"
            },
            success : function(data, textStatus, jqXHR) {
                $.each(data, function(index) {
                    var tmpUser                 = new ChatUserInfo();
                    tmpUser.Id                  = data[index].ID;
                    tmpUser.RoomId              = 1;
                    tmpUser.Name                = data[index].FirstName+' '+data[index].LastName;
                    tmpUser.Email               = data[index].Email;
                    tmpUser.ProfilePictureUrl   = "";
                    tmpUser.Status              = data[index].Online;
                    $users.push(tmpUser);
                });
            }
        });

        this.users = $users;
ehrenberg commented 8 years ago

My solution:

    DemoServerAdapter.prototype.getUserList = function(roomId, conversationId, done) {
        console.log("DemoServerAdapter: getUserList");

        var tmpUsers    = new Array();
        $.ajax({
            url : "/plugins/messages/ajax.php",
            type : "POST",
            dataType: 'json',
            data : {type : "get_userlist"},
            context:this,
            success : function(data) {
                var $t = new Array();
                $.each(data, function(index) {
                    console.log(index);
                    var tmpUser                 = new ChatUserInfo();
                    tmpUser.Id                  = data[index].ID;
                    tmpUser.RoomId              = 1;
                    tmpUser.Name                = data[index].FirstName+' '+data[index].LastName;
                    tmpUser.Email               = data[index].Email;
                    tmpUser.ProfilePictureUrl   = "";
                    tmpUser.Status              = data[index].Online;
                    tmpUsers[index] = tmpUser;
                });
                this.users = tmpUsers;
                if (roomId == DemoAdapterConstants.DEFAULT_ROOM_ID) {
                    done(this.users);
                    return;
                }
                throw "The given room or conversation is not supported by the demo adapter";
            }
        });

    };