Skip to content

Instantly share code, notes, and snippets.

@adtaylor
Created March 7, 2012 17:45
Show Gist options
  • Save adtaylor/1994650 to your computer and use it in GitHub Desktop.
Save adtaylor/1994650 to your computer and use it in GitHub Desktop.
# 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