diff --git a/jquery-plugin-patterns/amd-commonjs/pluginCore.js b/jquery-plugin-patterns/amd-commonjs/pluginCore.js index d4835a8..54ef44b 100644 --- a/jquery-plugin-patterns/amd-commonjs/pluginCore.js +++ b/jquery-plugin-patterns/amd-commonjs/pluginCore.js @@ -1,4 +1,4 @@ -// Module/Plugin core +// 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 @@ -7,41 +7,76 @@ // // Note that dependencies can just as easily be declared if required // and should work as demonstrated earlier with the AMD module examples. - -(function ( name, definition ){ - var theModule = definition(), - // this is considered "safe": - hasDefine = typeof define === 'function' && define.amd, - // hasDefine = typeof define === 'function', - hasExports = typeof module !== 'undefined' && module.exports; - - if ( hasDefine ){ // AMD Module - define(theModule); - } else if ( hasExports ) { // Node.js Module - module.exports = theModule; - } else { // Assign to common namespaces or simply the global object (window) - (this.jQuery || this.ender || this.$ || this)[name] = theModule; - } -})( 'core', function () { - var 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 = function(el,strColor){ - if(this.jQuery){ - jQuery(el).css('background', strColor); +(function(name, definition) { + var theModule = definition(), // this is considered "safe": + hasDefine = typeof define === 'function' && define.amd, // hasDefine = typeof define === 'function', + hasExports = typeof module !== 'undefined' && module.exports; + if (hasDefine) { // AMD Module + define(theModule); + } else if (hasExports) { // Node.js Module + module.exports = theModule; + } else { // Assign to common namespaces or simply the global object (window) + (this.jQuery || this.ender || this.$ || this)[name] = theModule; + console.log((window.jQuery)[name]); } - } - return { - highlightAll:function(){ - module.highlight('div', module.highlightColor); - } - }; - -}); \ No newline at end of file +})('app', function() { + + var core = this; + + var defaults = { // default settings + name: '123' + }; + + var opts = {}; // extend options + + var $document = $(document), + $window = $(window), + $html = $('html'); + + core.plugins = []; + + var base = { + eventnames: { + scroll: 'myScroll', + resize: 'myResize' + }, + scrollTop: 0, + highlightColor: 'yellow', + errorColor: 'red' + }; + + core.base = base; // set public base + + // private + var setHandler = function(){ + var _onScroll = function(){ + base.scrollTop = $document.scrollTop(); + $document.trigger(base.eventnames.scroll); + }; + $document.on('scroll resize', _onScroll); + }; + + // define the core base here and return the public API + core.init = function(options) { + opts = $.extend({}, defaults, options); + console.log(opts); + setHandler(); + }; + + // This is the highlight method used by the core highlightAll() + // method and all of the plugins highlighting elements different + // colors + core.highlight = function(el, strColor) { + $(el).css('background', strColor); + }; + + return { + init: function(options) { + core.init(options); + }, + highlight: function() { + core.highlight(el, strColor); + } + }; + +}); diff --git a/jquery-plugin-patterns/amd-commonjs/pluginExtension.js b/jquery-plugin-patterns/amd-commonjs/pluginExtension.js index 590e6b4..2c930c1 100644 --- a/jquery-plugin-patterns/amd-commonjs/pluginExtension.js +++ b/jquery-plugin-patterns/amd-commonjs/pluginExtension.js @@ -1,16 +1,13 @@ -// Extension to module core - -(function ( name, definition ) { +// Extension to module core +(function(name, definition) { var theModule = definition(), hasDefine = typeof define === 'function', hasExports = typeof module !== 'undefined' && module.exports; - - if ( hasDefine ) { // AMD Module + if (hasDefine) { // AMD Module define(theModule); - } else if ( hasExports ) { // Node.js Module + } else if (hasExports) { // Node.js Module module.exports = theModule; } else { // Assign to common namespaces or simply the global object (window) - // account for for flat-file/global module extensions var obj = null; var namespaces = name.split("."); @@ -24,21 +21,54 @@ } obj = scope[packageName]; } - } -})('core.plugin', function () { +})('app.inview', function() { + + var base = this.base; + + var $document = $(document), + $window = $(window), + $html = $('html'); + + var onLoad = function(){ + + }; + var onScroll = function(){ + console.log(base); + }; + var onResize = function(){ + + }; + var init = function(){ + $document.on(base.eventnames.load, onLoad); + $document.on(base.eventnames.scroll, onScroll); + $document.on(base.eventnames.resize, onResize); + }; + + init(); + + var test = function(){ + console.log('test'); + }; + + + + // console.log(base); // Define your module here and return the public API. // This code could be easily adapted with the core to // allow for methods that overwrite and extend core functionality // in order to expand the highlight method to do more if you wish. return { - setGreen: function ( el ) { + test: function(){ + test(); + }, + setGreen: function(el) { highlight(el, 'green'); }, - setRed: function ( el ) { - highlight(el, errorColor); + setRed: function(el) { + highlight(el, base.errorColor); } }; -}); \ No newline at end of file +}); diff --git a/jquery-plugin-patterns/amd-commonjs/usage.html b/jquery-plugin-patterns/amd-commonjs/usage.html index 92fd88d..e7b12e4 100644 --- a/jquery-plugin-patterns/amd-commonjs/usage.html +++ b/jquery-plugin-patterns/amd-commonjs/usage.html @@ -5,42 +5,60 @@
- - - - + + - // Our plugin 'core' is exposed under a core namespace in - // this example, which we first cache - var core = $.core; + - - - + -