Replacing SignalR callbacks doesn't work -
update: simplified error case further , filed issue on signalr's github page. can see issue here: https://github.com/signalr/signalr/issues/1828. bug has nothing angularjs.
i have project uses asp.net , signalr on backend , angularjs frontend. works fine except when move within angularjs , controllers created again, cannot replace original signalr callback methods.
here code controller:
function signalrproblemctrl($scope) { var bar = $.connection.bar; $.connection.hub.start(); $scope.currentmessage = ""; $scope.messages = []; $scope.messages.push("hello!"); var date = new date(); bar.client.addmessage = function (message) { $scope.$apply(function () { console.log("callback set on " + date + ": " + message); $scope.messages.push(message); }); }; $scope.send = function () { bar.server.send($scope.currentmessage, "extra data"); }; } signalrproblemctrl.$inject = ['$scope'];
on first visit controller can send , receive messages , angular bindings work fine. console has output:
callback set on fri apr 05 2013 14:37:13 gmt+0300 (e. europe daylight time): first page load controllers.js:37
this , dandy. problem arises after moving page , vising signalrproblemctrl again. time can send , receive messages, message gets written console bindings not updated. output in console points root cause:
callback set on fri apr 05 2013 14:37:13 gmt+0300 (e. europe daylight time): second page load controllers.js:37
so bindings don't updated because on second creation of controller first callback doesn't replaced. signalr messages still go first callback has $scope of previous instance of controller , view doesn't updated latest messages.
why this? why signalr hold on first callback?
ps. tried replacing callback immediately. code latest callback (one date2) used correctly.
var date = new date(); bar.client.addmessage = function (message) { $scope.$apply(function () { console.log("callback set on " + date + ": " + message); $scope.messages.push(message); }); }; var date2 = new date(); bar.client.addmessage = function (message) { $scope.$apply(function () { console.log("callback set on " + date2 + ": " + message); $scope.messages.push(message); }); };
after filing issue on github (https://github.com/signalr/signalr/issues/1828) got comment how solve this.
you can check out solution at: signalr (1.0.0-alpha2) hubs - can add client functions after connection has been started?
Comments
Post a Comment