我在应用程序中使用SignalR,以允许我的ASP.NET Server应用程序将数据推送到我的Angular SPA web客户端。在web应用程序中,我包含了jquery.signalR-2.2.0.js文件。我不在http://serverurl/signal/hubs上使用自动生成的代理。
Web应用程序有多个可重用的小部件(指令),每个小部件都有自己的独立作用域。同一个小部件/指令的多个实例需要注册一个回调,该回调可以由服务器的SignalR Hub执行,所以我有这样的功能:
var on = function (eventName, callback) {
proxy.on(eventName, callback);
};
这个函数在一个单独的服务中,它也有连接和代理对象,到目前为止一切都很好。。。
从一个负责小部件实例的角度控制器,上面的函数被调用如下:
var callback = function (){
console.log('Callback fired on controller with Id' + $scope.id)
}
proxyHub.on('myEvent',callback);
控制器处于独立的范围内,因此var回调对于每个控制器都是不同的对象。
我能够从不同的控制器为同一事件注册多个回调,并且一切仍按预期工作。
现在我需要从控制器实例中注销回调,所以我在单独的服务中有以下方法:
var off = function(eventName,callback) {
proxy.off(eventName,callback);
};
它从这样一个特定的控制器实例调用:
//callback is exactly the same variable as used in the .on function for this controller
hubProxy.off('myevent',callback);
问题来了:只有最后一个注册的回调被从事件中删除。所有其他已注册的回调仍在被调用。如果我再次尝试调用.off函数,则不会发生任何事情,并且我无法注销其他回调。
主要问题是:如何注销事件中的特定回调???
从hubs源代码来看,似乎不可能取消订阅单个事件的多个重复回调。
这是on
函数的源代码:
on: function (eventName, callback) {
/// <summary>Wires up a callback to be invoked when a invocation request is received from the server hub.</summary>
/// <param name="eventName" type="String">The name of the hub event to register the callback for.</param>
/// <param name="callback" type="Function">The callback to be invoked.</param>
var that = this,
callbackMap = that._.callbackMap;
// Normalize the event name to lowercase
eventName = eventName.toLowerCase();
// If there is not an event registered for this callback yet we want to create its event space in the callback map.
if (!callbackMap[eventName]) {
callbackMap[eventName] = {};
}
// Map the callback to our encompassed function
callbackMap[eventName][callback] = function (e, data) {
callback.apply(that, data);
};
$(that).bind(makeEventName(eventName), callbackMap[eventName][callback]);
return that;
},
正如您所看到的,"event:callback"对被保存到callbackMap
并绑定到hub。如果您多次调用它,callbackMap
将被相同的值覆盖,但回调将被绑定多次。
在off
功能中:
off: function (eventName, callback) {
/// <summary>Removes the callback invocation request from the server hub for the given event name.</summary>
/// <param name="eventName" type="String">The name of the hub event to unregister the callback for.</param>
/// <param name="callback" type="Function">The callback to be invoked.</param>
var that = this,
callbackMap = that._.callbackMap,
callbackSpace;
// Normalize the event name to lowercase
eventName = eventName.toLowerCase();
callbackSpace = callbackMap[eventName];
// Verify that there is an event space to unbind
if (callbackSpace) {
// Only unbind if there's an event bound with eventName and a callback with the specified callback
if (callbackSpace[callback]) {
$(that).unbind(makeEventName(eventName), callbackSpace[callback]);
// Remove the callback from the callback map
delete callbackSpace[callback];
// Check if there are any members left on the event, if not we need to destroy it.
if (!hasMembers(callbackSpace)) {
delete callbackMap[eventName];
}
} else if (!callback) { // Check if we're removing the whole event and we didn't error because of an invalid callback
$(that).unbind(makeEventName(eventName));
delete callbackMap[eventName];
}
}
return that;
},
该"事件:回调"对在off
函数的第一次调用时从callbackMap
中删除,并从集线器解除绑定(一次)。但在下一次调用中,"event:callback"对不再存在,所以所有其他事件仍然绑定到hub。
一种解决方案可以是从事件中删除所有回调,只使用myEventName
调用off
函数,而不指定回调。但我不确定这是否适用于你的情况。