2

I have a route which is loading a page based on the parameters:

.when('/:num/:den', {
templateUrl : 'app/views/templates/home.html',
controller :"ProductContoller",
controllerAs: "product",
reloadOnSearch : false });

The problem is any time that I reload the route, there is a new instance of the controller along with all previous instances still running. I have Socket.IO listeners in my controller to get some data from server in case of any update.

Babak
  • 615
  • 6
  • 15
  • 1
    Are you sure you don't just need to clean up your socket.io listeners? If you listen for the destroy event on scope, you can tidy up any listeners there :) It will look something like `$scope.$on('destroy', function( /* close your listener(s) here */ ) {});` – Dave Cooper Nov 14 '16 at 13:45
  • The problem is, there is no destroy event coming – Babak Nov 16 '16 at 11:03
  • How are your other controller instances running? – Dave Cooper Nov 16 '16 at 12:36
  • I just check them and they all stay running after changing the routes! – Babak Nov 17 '16 at 12:54
  • 1
    How do you check them? – Dave Cooper Nov 17 '16 at 15:33
  • Found the problem, first I was looking for destroy event! forgetting $, "$destroy". Also I had a bad mix up in my Socket service, in fact I was passing references of controllers to an object array in my Socket service. Corrected that and now I get the $destroy event, and cleaning up socket listeners. Thank you for your follow up. – Babak Nov 17 '16 at 16:47
  • If I create an answer with my original comment I made on this, any chance you'll mark it as the correct answer, since it was what you were after all along? :) – Dave Cooper Nov 17 '16 at 17:13
  • Oh, ignore that - you answered your own question! Thought someone external was stealing my fake internet points! – Dave Cooper Nov 17 '16 at 17:13
  • :D I didn't get what do you mean, but if I did something wrong or against rules here, I'm sorry ;) – Babak Nov 20 '16 at 21:53
  • 1
    No you didn't :) It's all good! Glad you solved your problem and wrote up a good quality answer too. – Dave Cooper Nov 21 '16 at 09:25

1 Answers1

2

In case of anybody else have the same problem, I was using Socket.IO, and it's listeners were still running so I added socket Emitter method, removeAllListeners, which is the same method as removeListener, removeListenerEvent and off. Now when I get "$destroy" event I am removing all those listeners by just passing no arguments to the method, or I can remove specific ones by stating the name of the events or even a specific handler by stating name of the event and name of the handler function.

Following is the code from Socke.IO source!

Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn)

and this is how I passed all the needed methods to my Service:

.factory('socketio', ["$rootScope", function ($rootScope) {
        var socket = io.connect();
        console.log(socket);
        return {
            on : function (eventName, callback) {
                socket.on(eventName, function () {
                    var args = arguments;
                    $rootScope.$apply(function () {
                        callback.apply(socket, args);
                    });
                });

            },
            removeAllListeners : function (eventName, callback) {
                socket.removeAllListeners(eventName, callback);
            },

            emit : function (eventName, data, callback) {
                socket.emit(eventName, data, function () {
                    var args = arguments;
                    $rootScope.apply(function () {
                        if (callback)
                            callback.apply(socket, args);
                    });
                });

            }

        };

    }
]);
Babak
  • 615
  • 6
  • 15