Created
March 7, 2012 17:45
-
-
Save adtaylor/1994650 to your computer and use it in GitHub Desktop.
Coffeescript conversion of https://github.com/addyosmani/jquery-plugin-patterns/blob/master/amd%2Bcommonjs/pluginCore.js#L27
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
# Module/Plugin core | |
# Note: the wrapper code you see around the module is what enables | |
# us to support multiple module formats and specifications by | |
# mapping the arguments defined to what a specific format expects | |
# to be present. Our actual module functionality is defined lower | |
# down, where a named module and exports are demonstrated. | |
# | |
# Note that dependencies can just as easily be declared if required | |
# and should work as demonstrated earlier with the AMD module examples. | |
(( name, definition ) -> | |
theModule = definition() | |
# this is considered "safe": | |
hasDefine = typeof define is 'function' and define.amd | |
# hasDefine = typeof define === 'function', | |
hasExports = typeof module isnt 'undefined' and module.exports | |
if ( hasDefine ) # AMD Module | |
define(theModule) | |
else if ( hasExports ) # nodejs module | |
module.exports = theModule | |
else # Assign to common namespaces or simply the global object (window) | |
(this.jQuery || this.ender || this.$ || this)[name] = theModule | |
)( 'core', () -> | |
module = this | |
module.plugins = [] | |
module.highlightColor = "yellow" | |
module.errorColor = "red" | |
# define the core module here and return the public API | |
# This is the highlight method used by the core highlightAll() | |
# method and all of the plugins highlighting elements different | |
# colors | |
module.highlight = (el,strColor) -> | |
if this.jQuery then jQuery(el).css('background', strColor); | |
# Expose public methods | |
{ | |
highlightAll : () -> | |
module.highlight('div', module.highlightColor) | |
plugins : | |
module.plugins | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment