callback capturing and firing with additional params in roslibjs

asked 2022-04-13 09:22:59 -0600

ravijoshi gravatar image

I need the DOM element inside my subscriber callback function. Therefore, I am thinking to write a wrapper on the top of my subscriber. Please see a sample code below (the code doesn't work though):

function Subscriber(element, topic) {
    var listener = new ROSLIB.Topic({
        ros: ros,
        name: topic,
        messageType: "std_msgs/String"
    });
    listener.on("subscribe", this.handleEvent);
    this.listener = listener;
    this.element = element;
}

Subscriber.prototype.handleEvent = function (event) {
    switch (event.type) {
        case "subscribe":
            var customEvent = new CustomEvent("subscribe", { 
                details: { 
                    msg: message, 
                    e: element
            }});
            this.element.dispatchEvent(customEvent);
    }
};

var element = document.getElementById("myDiv");
var topic = "/listener";
var subscriber = new Subscriber(element, topic);
subscriber.handleEvent(function (event) {
    // do crazy things here. for example replace the text
    event.details.e.innerHTML = event.details.msg.data;
});

How do get the specified DOM element inside my subscriber callback?

edit retag flag offensive close merge delete