I’m using Ionic and on a view load I’m trying to add a listener, and on a view unload I’m trying to remove a listener.
ionViewDidLoad()
{
this.serverEvents.client.addListener("ProductDetailsChanged", this.productDetailsChangedHandler);
}
ionViewWillLeave()
{
this.serverEvents.client.removeListener("ProductDetailsChanged", this.productDetailsChangedHandler);
}
The problem is, ‘this’ gets lost in my handler. I could wrap the function and return a closure with a var that = this;
kind of hack, but then I can’t use removeListener properly because that’ll be two different functions/objects.
Any thoughts on what I can do?
Here’s my handler:
private productDetailsChangedHandler(msg)
{
console.log("productDetailsChangedHandler", msg);
let evt = JSON.parse(msg.json);
console.log("evt", evt);
console.log("product", this);
//if(evt.productId == this.product.productId)
//{
// console.log("updating product", msg, evt);
// this.product = angular.merge(this.product, evt.details);
//}
}
The log with ‘this’ in it returns undefined.