FANDOM


  • Hi there! I am just trying to barely dip my toes into some MediaWiki coding, although it is slow-going and quickly confusing because neither FANDOM nor MediaWiki itself have any tutorials that I can find on how to use their libraries.

    Anyway, I just have a quick question about a basic concept: Here is my starting code. When I just have an event that uses mw.hook().add(), it is supposed to "Register a hook handler" according to the documentation (in this case, it is registering a handler to the 'wikipage.content' event). I would not expect this handler to actually fire when the Event occurs, because I have not added an mw.hook().fire() line. Yet in reality when I check the console, the event fires twice: (see picture).

    Even more strangely, when I do add the mw.hook().fire() method (so that my code looks like this), it causes the mw.hook().add() event to only fire once! And the handler inside the mw.hook().fire() event doesn't fire at all.

    So why is this? What is the purpose of mw.hook().fire() if the .add() method already fires its handler on the event occurrence?

    If you can explain the Media Wiki stuff in simple term for me, that would be great because even though I have tons of experience with JS and Lua, I have never used the MediaWiki libraries before now :) Thank you very much!

      Loading editor
    • wikipage.content is fired when the page loads (and when you preview your edit and other things happen too if I remember correctly), so your function is running when the page and etc load, logging your string to the console. The use of .fire() is for scripts, so you can have events where things happen (eg, your script is done loading) and other code or other user's code can listen for those events and run their code when the event happens. (see w:c:dev:MediaWiki:FastDelete/code.js for an example of .fire())

        Loading editor
    • also, make sure that you did read and understand the documentation. it seems like you have missed "memorized" part of it.

        Loading editor
    • Sophiedp wrote: wikipage.content is fired when the page loads (and when you preview your edit and other things happen too if I remember correctly), so your function is running when the page and etc load, logging your string to the console. The use of .fire() is for scripts, so you can have events where things happen (eg, your script is done loading) and other code or other user's code can listen for those events and run their code when the event happens. (see w:c:dev:MediaWiki:FastDelete/code.js for an example of .fire())

      Okay, so I guess the part I don't understand is what is the difference between .add() and .fire(), then? What would be different in w:c:dev:MediaWiki:FastDelete/code.js if on line 62 (the first occurence of .fire()) they used .add() instead? It still would have mw.hook('fastdelete.init') preceding it.


      Fngplg wrote: also, make sure that you did read and understand the documentation. it seems like you have missed "memorized" part of it.

      What do you mean?

        Loading editor
    • Emitewiki2 wrote:

      Sophiedp wrote: wikipage.content is fired when the page loads (and when you preview your edit and other things happen too if I remember correctly), so your function is running when the page and etc load, logging your string to the console. The use of .fire() is for scripts, so you can have events where things happen (eg, your script is done loading) and other code or other user's code can listen for those events and run their code when the event happens. (see w:c:dev:MediaWiki:FastDelete/code.js for an example of .fire())

      Okay, so I guess the part I don't understand is what is the difference between .add() and .fire(), then? What would be different in w:c:dev:MediaWiki:FastDelete/code.js if on line 62 (the first occurence of .fire()) they used .add() instead? It still would have mw.hook('fastdelete.init') preceding it.

      The script fires the hook that other JS can wait for (using add), the mw.hook('fastdelete.init') part just is what hook you're using.

        Loading editor
    • Sophiedp wrote: The script fires the hook that other JS can wait for (using add), the mw.hook('fastdelete.init') part just is what hook you're using.

      Ohhh, so you're saying that .fire() forces the code to act as if the hook was actually fired instead of waiting for the hook to fire naturally?

      e.g. the line mw.hook.('wikipage.content').fire() would fire the 'wikipage.content' event immediately (and thus triggering any previously-defined mw.hook('wikipage.content').add() functions) instead of waiting for the 'wikipage.content' to fire naturally (when the page loads, or when you preview your edit, or whatever)?

      Is that it?

        Loading editor
    • Yeah I think thats what it would do (I haven't actually tested if you can force wikipage.content to fire), but I believe that would be how it would work. (it also works for hooks that wouldn't fire otherwise, eg, fastdelete.init doesn't exist until its fired by FastDelete's JS)

        Loading editor
    • Sophiedp wrote: Yeah I think thats what it would do (I haven't actually tested if you can force wikipage.content to fire), but I believe that would be how it would work. (it also works for hooks that wouldn't fire otherwise, eg, fastdelete.init doesn't exist until its fired by FastDelete's JS)

      Ah, okay, perfectly understood! Thanks!

      One last side note, does that mean no function can be defined inside of .fire()? Like, you couldn't have mw.hook('some-hook').fire(function($content){//do some stuff});? Functions would have to defined in the mw.hook.add() instead?

        Loading editor
    • since you expressing misunderstanding of basic principles, i'd suggest to read the documentation. you have linked it above.

      short version: it is a subscription. one can subscribe to an event (via .add) or emit an event (via .fire). both .add and .fire serves to different purposes. one can't use .fire to subscribe to the event or .add to emit the event.

        Loading editor
    • Emitewiki2 wrote:

      Sophiedp wrote: Yeah I think thats what it would do (I haven't actually tested if you can force wikipage.content to fire), but I believe that would be how it would work. (it also works for hooks that wouldn't fire otherwise, eg, fastdelete.init doesn't exist until its fired by FastDelete's JS)

      Ah, okay, perfectly understood! Thanks!

      One last side note, does that mean no function can be defined inside of .fire()? Like, you couldn't have mw.hook('some-hook').fire(function($content){//do some stuff});? Functions would have to defined in the mw.hook.add() instead?

      I don't think that would work, but you could always test it with your own event if you want.

        Loading editor
    • Sophiedp wrote: I don't think that would work, but you could always test it with your own event if you want.

      Ah alright perfect, thanks so much for all the help! This really helped clear up my confusion a ton and was very beneficial to me as I'm learning this code! :)

        Loading editor
    • Fngplg wrote: since you expressing misunderstanding of basic principles, i'd suggest to read the documentation. you have linked it above.

      Thank you for your concern. I have indeed read the documentation I linked above before posting it here. Unfortunately, it is written as reference manual (as it should be) and not as a tutorial, and there are no tutorials available online (that I am able to find). So when something read in the manual does not make sense and personal testing is not able to clear up the confusion, this forum is a great resource for asking questions to real people to help fellow wiki coders. I'm really happy for you if your brain works in a way that it is immediately able to grasp every concept perfectly just by reading the manual over, unfortunately mine does not work in the same way :) It is good that we have people like you who can help spread understanding to those of us who don't quite get everything after reading the manual.

      Fngplg wrote: short version: it is a subscription. one can subscribe to an event (via .add) or emit an event (via .fire). both .add and .fire serves to different purposes. one can't use .fire to subscribe to the event or .add to emit the event.

      Ah, okay, thank you!

        Loading editor
    • A FANDOM user
        Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message
Community content is available under CC-BY-SA unless otherwise noted.