Skip to content

Instantly share code, notes, and snippets.

@carldanley
Last active December 22, 2015 00:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carldanley/6389161 to your computer and use it in GitHub Desktop.
Save carldanley/6389161 to your computer and use it in GitHub Desktop.
Example of WP modules object
// new modules object introduction
( function( window, undefined ) {
window.wp = window.wp || {};
window.wp.modules = window.wp.modules || {};
} )( window );
// example Mediator module
wp.modules.Mediator = ( function( window, undefined ) {
function Mediator() {
this.topics = {};
}
Mediator.prototype.publish = function publish() {
//...
};
Mediator.prototype.subscribe = function subscribe() {
//...
};
Mediator.prototype.unsubscribe = function unsubscribe() {
//...
};
return Mediator;
} )( window );
// now we introduce a global version of the Mediator that can be used to communicate
// across features of WordPress, ie. - Heartbeat API to Media Modal
wp.mediator = new wp.modules.Mediator();
// if someone wanted to use the mediator within their individual component, say media modal, they could
// do so like this:
( function( window, undefined ) {
// all the fake module code for media modal goes here within a public scope
// we need a mediator so instead of re-implementing, we can make use of the modules already there
// in the event that i'm unsure of what modules there are, I can `console.log( window.wp.modules );`
var myPrivateFeatureMediator = new window.wp.modules.Mediator();
// then internally, we can use this mediator to communicate with the different pieces of this
// feature so we don't pollute the global traffic via the use of `wp.mediator`
} )( window );
@kadamwhite
Copy link

Going to think about this for a bit, but in principle I prefer

( function( wp, undefined ) {

  // Clunky to have to `window.` for everything; if you're
  // passing something in you should pass in the thing
  // that you actually want imo

} )( window.wp );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment