User:TheDJ/ExampleGadget.js

/** * This is an example gadget * It has no goal other than to show you techniques you might want to use in your own gadgets */ // 1: Module pattern // This has two basic functions. Creating "functional scoping" // and to rename our global variables to shorter variable names. // mw and $ are also declared globally, but due to their length there is a higher // chance of collisions and especially $ is often used by other libraries as well. ( function( mw, $ ) {	// 2: Any variables you want to use for the duration of the module's lifetime	// can be kept here. They are the global state of the module.	var state;	// 3: Functions that you use internally within the module should be defined	// as named functions. Try to avoid declaring functions inside functions,	// but especially avoid declaring them inside loops. 	function aPrivateFunction {	}

// TODO // 1: document.ready // 2: private and public functions // 3: addportletlink // 4: resource loader modules (and loader) // 5: require pattern // 6: api requests // 7: preferences // 7: click handler preventDefault // 8: using external cdnjs libraries

// 1: End of 1 // Here we execute the anonymous function preceding it, and pass it global // variables that we want it to be able to use. These are supposed to be the // 'only' global variables you use, making it easy to check for accidental global // variable declarations } )( mediaWiki, jQuery );