Remove grp.listener - DGrandes - 07.03.2023
Hi,
I´m doing a widget with dynamic object address and I need to remove listeners when I close widget or when I open it again with another address.
Code:
// Declare vars
var ObjEscucha = "0/0/0";
var MainObjectSplitted = [];
var DirOriginales = ["8/1/102","8/1/103"];
var DirWrNuevas = ["1/1/1","1/1/2"];
var DirWrSustituidas = []
var DirEstadosReales = [["0/2/0","0/2/1"],["0/3/1","0/3/2"]]
var DirEstSustituidas = [["Es la 0/2/0","Es la 0/2/1"],["H","ADIOS"]]
function actualizarEstados(a,b) {
return function (obj) {
console.log(DirEstSustituidas[a][b])
};
}
function removelistener(src) {
var el = $(src) // source element reference
, addr = el.data('status-object') // group address of status object
, id = Scada.encodeGroupAddress(addr) // address to id
, obj = objectStore.objects[ id ]; // get object's store reference
if (obj) {
// find listener that is attached to the source element
console.log(addr)
$.each(obj.listeners, function(index, listener) {
// remove the listener
if (listener.bind.el && el.is(listener.bind.el)) {
obj.listeners.splice(index, 1);
}
});
}
}
// Detect adresses linked to widget button and create arrays
$(".screen_widget").on("click", function(e) {
ObjEscucha = $(this).data("object");
for (var i = 0; i < DirOriginales.length; i++) {
if (ObjEscucha == DirOriginales[i]){
DirWrSustituidas = DirWrNuevas[i]
//I NEED TO REMOVE THIS LISTENER WHEN I OPEN AGAIN THE WIDGET
for (var j = 0; j < DirEstadosReales[i].length; j++) {
grp.listen(DirEstadosReales[i][j], actualizarEstados(i,j));
}
}
}
});
How can i do that?
Thanks!
RE: Remove grp.listener - admin - 07.03.2023
Same arguments as for grp.listen:
Code: function removelistenerfn(addr, fn) {
var id = Scada.encodeGroupAddress(addr);
var listeners = objectStore.listeners[ id ] || [];
$.each(listeners, function(index, listener) {
if (listener.fn == fn) {
listeners.splice(index, 1);
}
});
}
RE: Remove grp.listener - DGrandes - 07.03.2023
(07.03.2023, 10:57)admin Wrote: Same arguments as for grp.listen:
Code: function removelistenerfn(addr, fn) {
var id = Scada.encodeGroupAddress(addr);
var listeners = objectStore.listeners[ id ] || [];
$.each(listeners, function(index, listener) {
if (listener.fn == fn) {
listeners.splice(index, 1);
}
});
}
And what do i have to put in "fn". When I close the widget I need to remove the listener that I´ve created when I open it.
Thanks
RE: Remove grp.listener - admin - 07.03.2023
Same as for grp.listen. First argument is the group address, second argument is the callback function.
RE: Remove grp.listener - DGrandes - 07.03.2023
(07.03.2023, 11:40)admin Wrote: Same as for grp.listen. First argument is the group address, second argument is the callback function.
I´ve done that but it doesn´t work
callback function is " actualizarEstados(i,j) " and addreses are in the array "DirEstadosReales[i][j]"
Code: // Detect adresses linked to widget button and create arrays
$(".screen_widget").on("click", function(e) {
removelistenerfn("0/2/1")
ObjEscucha = $(this).data("object");
for (var i = 0; i < DirOriginales.length; i++) {
//REMOVE LISTENERS
for (var j = 0; j < DirEstadosReales[i].length; j++) {
//console.log(DirEstadosReales[i][j])
removelistenerfn(DirEstadosReales[i][j],actualizarEstados(i,j))
}
if (ObjEscucha == DirOriginales[i]){
DirWrSustituidas = DirWrNuevas[i]
//I NEED TO REMOVE THIS LISTENER WHEN I OPEN AGAIN THE WIDGET
for (var j = 0; j < DirEstadosReales[i].length; j++) {
grp.listen(DirEstadosReales[i][j], actualizarEstados(i,j));
}
}
}
});
RE: Remove grp.listener - admin - 07.03.2023
This won't work because each time you call actualizarEstados a new function is created.
If you don't have any other code modifying the listeners array you can simply remove the last listener that has been registered like this:
Code: function removelistenerlast(addr) {
var id = Scada.encodeGroupAddress(addr);
var listeners = objectStore.listeners[ id ] || [];
listeners.pop();
}
RE: Remove grp.listener - DGrandes - 07.03.2023
(07.03.2023, 12:03)admin Wrote: This won't work because each time you call actualizarEstados a new function is created.
If you don't have any other code modifying the listeners array you can simply remove the last listener that has been registered like this:
Code: function removelistenerlast(addr) {
var id = Scada.encodeGroupAddress(addr);
var listeners = objectStore.listeners[ id ] || [];
listeners.pop();
}
Ok thanks!
|