I'm attempting to build a Dashboard with widgets (built as directives). I'd like to at a later stage have the ability to dynamically add widgets, but I'd only like the active widgets (and hubs) to receive data, thus if a widget isn't active i don't want the hub to be registered.
Eg for the duration of the user using the app there will be a global signalR context, as well as page specific ones, which will be spawned/destroyed as needed.
This is my best try ATM... which isn't working
factory
(function () {
'use strict';
angular.module('app').factory('hubFactory', ['permissionSvc', 'common', hubFactory]);
function hubFactory(permissionSvc, common) {
var connection = [];
return {
context: function (name) {
if (!connection[name]) {
var conn = $.connection;
conn.hub.url = common.serviceUrl + '/signalr';
conn.hub.start();
conn.prototype.addHub = function (hubName, options) {
// defaults
var opts = {
authorize: true
};
angular.extend(opts, options);
if (opts.authorize) {
permissionSvc.createPrefilter();
}
var hub = conn[hubName];
var run = function () {
hub.server.initialise();
};
return {
hub: hub,
run: run
};
};
connection[name] = conn;
}
return connection[name]();
}
};
}
})();
widget directive-controller
controller: function ($scope) {
var context = hubFactory.context('dashboard');
var instance = context.addHub('agreementApprovalsHub');
instance.hub.client.getAllUnapprovedAgreements = function (data) {
$scope.data = data;
};
instance.run();
}
The following needs to be called in the done method of the start... but what if i want to, start up the connection on page load, and then append hubs as needed (or is my thinking wrong?)
There are various problems:
var run = function () { hub.server.initialise(); };
vs
var run = function () { conn.hub.start().done(function() { hub.server.initialise(); }); };
TBH, it feels like i'm butchering the code, and need to probably start from scratch at this stage.. I'm thoroughly confused on how to go about it all, and if its even possible.