Last active
December 22, 2015 00:28
-
-
Save carldanley/6389161 to your computer and use it in GitHub Desktop.
Example of WP modules object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Going to think about this for a bit, but in principle I prefer