Frage

I'm working with a 3rd party product where I am extending the UI with my own custom functionality. Within part of that I need to call an event after the UI has been updated with an AJAX call. Luckily the app fires a call to a Custom Event using the Prototype JS library after the call is complete, like this:

$(document.body).fire("ns:customevent");

If I add my own custom event with the same name then this works as expected

$(document).observe("ns:customevent", function(event) {
    //do custom stuff here
});

[I could not get $(document.body).observe() to work here but I don't think that really matters.]

My concern here is that there may be other parts of the app that have registered functions against that event, and I am (blindly) overwriting them with my own, which will lead to issues further down the line.

Does Prototype append custom functions even though they have the same name or does it in fact overwrite them? If it does overwrite them then how can I append my function to anything that is already existing? Is there anyway of viewing what

I would imagine something like this, but I hardly know Protoype and my JS is very rusty these days.

var ExistingCustomEvent = $(document.body).Events["ns:customevent"];
$(document).observe("ns:customevent", function(event) {
     ExistingCustomEvent();
    //do custom stuff here
});

I can't add my event handler or add in code to call my own function, I want to try avoiding the 3rd party library (if that would even be possible).

Thanks.

War es hilfreich?

Lösung

As an FYI for anyone else that stumbles upon this question, following the comment from Pointy it turns out that Prototype does append the functions to the custom event.

I verified this by trying the following and both alerts fired.

$(document).observe("ns:customevent", function(event) {
     alert("ALERT 1");
});
$(document).observe("ns:customevent", function(event) {
     alert("ALERT 2");
});

Great :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top