Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

AMD pubsub: Difference between revisions

From MoodleDocs
Created page with "{{Moodle 3.6}} = What is this? = A simply module to do event subscription and publishing in JavaScript. It was added to avoid the need to use jQuery and the DOM. = Why would..."
 
This page will not be migrated to new devdocs
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:WillNotMigrate}}
{{Moodle 3.6}}
{{Moodle 3.6}}


= What is this? =
= What is this? =
A simply module to do event subscription and publishing in JavaScript. It was added to avoid the need to use jQuery and the DOM.
A simple module to do event subscription and publishing in JavaScript. It was added to avoid the need to use jQuery and the DOM.


= Why would I use it? =
= Why would I use it? =
Line 8: Line 10:


= Example =
= Example =
<code javascript>
<syntaxhighlight lang="javascript">
// Module A.
// Module A.
require(['core/pubsub'], function(PubSub) {
require(['core/pubsub'], function(PubSub) {
Line 20: Line 22:
     });
     });
});
});
</code>
</syntaxhighlight>

Latest revision as of 15:34, 7 February 2025


Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.


Moodle 3.6


What is this?

A simple module to do event subscription and publishing in JavaScript. It was added to avoid the need to use jQuery and the DOM.

Why would I use it?

Allows modules to communicate with one another indirectly without the need to include jQuery or touch the DOM.

Example

// Module A.
require(['core/pubsub'], function(PubSub) {
    PubSub.publish('example-event', someData);
});

// Module B.
require(['core/pubsub'], function(PubSub) {
    PubSub.subscribe('example-event', function(someData) {
        console.log('Received', someData);
    });
});