forked from JustMaier/angular-signalr-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalr-hub.js
36 lines (35 loc) · 1.05 KB
/
signalr-hub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
angular.module('SignalR', [])
.constant('$', $)
.factory('Hub', ['$', function ($) {
//This will allow same connection to be used for all Hubs
//It also keeps connection as singleton.
var globalConnection = $.hubConnection();
return function (hubName, listeners, methods) {
var Hub = this;
Hub.connection = globalConnection;
Hub.proxy = Hub.connection.createHubProxy(hubName);
Hub.on = function (event, fn) {
Hub.proxy.on(event, fn);
};
Hub.invoke = function (method, args) {
return Hub.proxy.invoke.apply(Hub.proxy, arguments)
};
if (listeners) {
angular.forEach(listeners, function (fn, event) {
Hub.on(event, fn);
});
}
if (methods) {
angular.forEach(methods, function (method) {
Hub[method] = function () {
var args = $.makeArray(arguments);
args.unshift(method);
return Hub.invoke.apply(Hub, args);
};
});
}
//Adding additional property of promise allows to access it in rest of the application.
Hub.promise = Hub.connection.start();
return Hub;
};
}]);