Jerry
Jerry

Reputation: 1782

Implementing SignalR client callback function with Typescript

I'm trying to make SignalR and Typescript work together.

I've got the following file:

/// <reference path="../../vendor/jquery/jquery.d.ts" />

interface SignalR {
    roleHub: IRoleHub;
} 

interface IRoleHub {
    server: IRoleHubServer;
    client: IRoleHubClient;
}

interface IRoleHubServer {
    getUserRoles();
}

interface IRoleHubClient {
    populateUserRoles(names: string[]) : IRoleHubClient;
}

The following class implements IRoleHub:

module Portal.App {
    export class Controller implements IRoleHub {
        role: Portal.App.Model.IRole;

        server: IRoleHubServer;
        client: IRoleHubClient;

        constructor(role: Portal.App.Model.IRole) {
            this.role = role;
            this.server = $.connection.roleHub.server;
            this.client = $.connection.roleHub.client;
        }

        getUserRoles() {
            this.server.getUserRoles();
        }

        populateUserRoles(roles: string[]) {
            console.log('populated');
        }
    }
}

I call getUserRoles in another file and my server side function is being invoked, however I'm struggling to implement the populateUserRoles client side callback.

In Javascript, I would have done:

connection.client.populateUserRoles = function (roles) {
    [...]
}

however my code above generates the following output, which is incorrect:

Controller.prototype.populateUserRoles = function (roles) {
                        console.log('populated');
                    };

I will appreciate any help with implementing this function.

Upvotes: 1

Views: 2626

Answers (1)

basarat
basarat

Reputation: 276085

Here is one way that should work :

   constructor(role: Portal.App.Model.IRole) {
        this.role = role;
        this.server = $.connection.roleHub.server;
        this.client = $.connection.roleHub.client;
        this.client.populateUserRoles = (roles)=> this.roles(roles);
    }

Upvotes: 1

Related Questions