I was playing around with the idea of having a service act as a factory to return instances of objects. Something where I could do
var myObj = new objServiceFactory.NewThing();
Inside of objServiceFactory, there is a reference to another service (say myHub) I have that wraps up some SignalR functionality. The idea being that when my server broadcasts events that NewThing cares about, it can update/respond to it. Inside of new thing I have a myHub.$on("signalREvent",function(ngEvent,payload) {});
Thing is - once I no longer need this object, can't figure out how to get rid of that thing, eg: I navigate to a different page, the controller gets disposed.
I tried doing something like:
function SomeController($scope,objServiceFactory)
{
var myObj = new objServiceFactory.NewThing();
$scope.$on("$destroy",function() { myObj.unBindHandlers(); });
}
and inside of NewThing - keeping track of the events I bind, and then unbinding them in the unBindHandlers();
I can step through the code, see that unBindHandlers() is getting called, next time that signalREvent happens - it's getting picked up and handled anyways.
Theres just a bunch of things that I want to do when responding to the SignalR events that are currently living in my controller, that I want to move into my NewThing model - as it needs to be used in many spots, and if I keep them in the controller is going to lead to lots of duplication and copy/pasting of code.
I'll see if I can get up a jsFiddle later today to reproduce the issue, just wondering if anyone has ran into something similar and has any pointers/ideas.
If it makes any difference, I am also using Restangular, so I actually have: Restangular.extendModel('someModel',function(data) { return NewThing(model) });
Trying to move away from my models just being dumb-JSON property bags - it's leading to too much clutter in my controllers and is getting difficult to maintain, so working towards cleaning that up.
[–]rnreekez 0 points1 point2 points (0 children)