Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

AMD pubsub: Difference between revisions

From MoodleDocs
No edit summary
m (Text replacement - "<code (.*)>" to "<syntaxhighlight lang="$1">")
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:


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

Latest revision as of 08:24, 15 July 2021

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);
    });
});