admin管理员组文章数量:1357276
I'm using SignalR in my application to allow my ASP.NET Server application to push data to my Angular SPA web clients. In the web application i've included the jquery.signalR-2.2.0.js file. I do not use the auto generated proxy on http(s)://serverurl/signalr/hubs.
The webapplication has multiple reusable widgets (directives), each with their own isolated scope. Multiple instances of the same widget/directive need to register a callback which can be executed by the server's SignalR Hub so i have a function like this:
var on = function (eventName, callback) {
proxy.on(eventName, callback);
};
This function is in a seperate service which also has the connection and proxy object, and until now everything is working fine...
From a angular controller,responsible for a widget instance, the above function gets called like this:
var callback = function (){
console.log('Callback fired on controller with Id' + $scope.id)
}
proxyHub.on('myEvent',callback);
The controllers are in isolated scope, so the var callback is a different object for each controller.
I am able to register multiple callbacks for the same event from different controllers, and everything is still working as expected.
Now i need to unregister a callback from a controller instance, so i have the following method in my separate service:
var off = function(eventName,callback) {
proxy.off(eventName,callback);
};
Which gets called from a specific controller instance like this:
//callback is exactly the same variable as used in the .on function for this controller
hubProxy.off('myevent',callback);
Here es the trouble: Only the last registered callback is removed from the event. All other registered callbacks still getting invoked. If i try to call the .off function again, nothing happens, and i am not able to unregister the other callbacks.
The main question is: How can i unregister a specific callback from an event???
I'm using SignalR in my application to allow my ASP.NET Server application to push data to my Angular SPA web clients. In the web application i've included the jquery.signalR-2.2.0.js file. I do not use the auto generated proxy on http(s)://serverurl/signalr/hubs.
The webapplication has multiple reusable widgets (directives), each with their own isolated scope. Multiple instances of the same widget/directive need to register a callback which can be executed by the server's SignalR Hub so i have a function like this:
var on = function (eventName, callback) {
proxy.on(eventName, callback);
};
This function is in a seperate service which also has the connection and proxy object, and until now everything is working fine...
From a angular controller,responsible for a widget instance, the above function gets called like this:
var callback = function (){
console.log('Callback fired on controller with Id' + $scope.id)
}
proxyHub.on('myEvent',callback);
The controllers are in isolated scope, so the var callback is a different object for each controller.
I am able to register multiple callbacks for the same event from different controllers, and everything is still working as expected.
Now i need to unregister a callback from a controller instance, so i have the following method in my separate service:
var off = function(eventName,callback) {
proxy.off(eventName,callback);
};
Which gets called from a specific controller instance like this:
//callback is exactly the same variable as used in the .on function for this controller
hubProxy.off('myevent',callback);
Here es the trouble: Only the last registered callback is removed from the event. All other registered callbacks still getting invoked. If i try to call the .off function again, nothing happens, and i am not able to unregister the other callbacks.
The main question is: How can i unregister a specific callback from an event???
Share Improve this question asked Dec 17, 2015 at 14:08 Jeroen1984Jeroen1984 1,6861 gold badge19 silver badges33 bronze badges1 Answer
Reset to default 8From the hubs source code it seems like it's not possible to unsubscribe multiple duplicate callbacks from a single event.
This is on
function from the source code:
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 enpassed function
callbackMap[eventName][callback] = function (e, data) {
callback.apply(that, data);
};
$(that).bind(makeEventName(eventName), callbackMap[eventName][callback]);
return that;
},
as you can see, the "event:callback" pair is saved to callbackMap
and bound to hub. If you call it multiple times, the callbackMap
will be overwritten with the same value, but the callback will be bound multiple times.
In the off
function:
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;
},
that "event:callback" pair is removed from callbackMap
on first call of the off
function and unbound from hub (once). But on next call, the "event:callback" pair is not there anymore, so all other events are still bound to hub.
One solution could be to remove all callbacks from the event, by just calling off
function with only myEventName
, and not specifying the callback. But I'm not sure, if this is applicable in your case.
本文标签: javascriptjQuery SignalR client off() function only removes last registered callbackStack Overflow
版权声明:本文标题:javascript - jQuery SignalR client .off() function only removes last registered callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074960a2586573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论