AMD pubsub: Difference between revisions
From MoodleDocs
Ryan Wyllie (talk | contribs) 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..." |
Ryan Wyllie (talk | contribs) No edit summary |
||
| Line 2: | Line 2: | ||
= What is this? = | = What is this? = | ||
A | 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? = | ||
Revision as of 06:17, 6 December 2018
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);
});
});