Skip to content

Instantly share code, notes, and snippets.

@peet86
Forked from simonsmith/amd-jquery-plugin.js
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save peet86/a4dff9ac996e731bb9dc to your computer and use it in GitHub Desktop.

Select an option

Save peet86/a4dff9ac996e731bb9dc to your computer and use it in GitHub Desktop.

Revisions

  1. @simonsmith simonsmith revised this gist Aug 25, 2014. 1 changed file with 44 additions and 45 deletions.
    89 changes: 44 additions & 45 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,61 +1,60 @@
    // UMD dance - https://github.com/umdjs/umd
    !function(root, factory) {
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    } else {
    factory(root.jQuery);
    }
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    } else {
    factory(root.jQuery);
    }
    }(this, function($) {
    'use strict';
    'use strict';

    // Default options
    var defaults = {

    // Default options
    var defaults = {

    };
    };

    // Constructor, initialise everything you need here
    var Plugin = function(element, options) {
    this.element = element;
    this.options = options;
    };
    // Constructor, initialise everything you need here
    var Plugin = function(element, options) {
    this.element = element;
    this.options = options;
    };

    // Plugin methods and shared properties
    Plugin.prototype = {
    // Reset constructor - http://goo.gl/EcWdiy
    constructor: Plugin,
    // Plugin methods and shared properties
    Plugin.prototype = {
    // Reset constructor - http://goo.gl/EcWdiy
    constructor: Plugin,

    someMethod: function() {
    someMethod: function() {

    }
    }
    }

    // Create the jQuery plugin
    $.fn.plugin = function(options) {
    // Do a deep copy of the options - http://goo.gl/gOSSrg
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    var $this = $(this);
    // Create a new instance for each element in the matched jQuery set
    // Also save the instance so it can be accessed later to use methods/properties etc
    // e.g.
    // var instance = $('.element').data('plugin');
    // instance.someMethod();
    $this.data('plugin', new Plugin($this, options));
    });
    };

    // Create the jQuery plugin
    $.fn.plugin = function(options) {
    // Do a deep copy of the options - http://goo.gl/gOSSrg
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    var $this = $(this);
    // Create a new instance for each element in the matched jQuery set
    // Also save the instance so it can be accessed later to use methods/properties etc
    // e.g.
    // var instance = $('.element').data('plugin');
    // instance.someMethod();
    $this.data('plugin', new Plugin($this, options));
    });
    };

    // Expose defaults and Constructor (allowing overriding of prototype methods for example)
    $.fn.plugin.defaults = defaults;
    $.fn.plugin.Plugin = Plugin;
    // Expose defaults and Constructor (allowing overriding of prototype methods for example)
    $.fn.plugin.defaults = defaults;
    $.fn.plugin.Plugin = Plugin;
    });



    // Usage example
    require(['jquery', 'jquery.plugin'], function($) {
    $('.test li').plugin({
    test: 'option1',
    test2: 'option2'
    });
    $('.test li').plugin({
    test: 'option1',
    test2: 'option2'
    });
    });
  2. @simonsmith simonsmith revised this gist Dec 4, 2013. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@

    // UMD dance - https://github.com/umdjs/umd
    !function(root, factory) {
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    @@ -8,15 +8,18 @@
    }(this, function($) {
    'use strict';

    // Default options
    var defaults = {

    };

    // Constructor, initialise everything you need here
    var Plugin = function(element, options) {
    this.element = element;
    this.options = options;
    };

    // Plugin methods and shared properties
    Plugin.prototype = {
    // Reset constructor - http://goo.gl/EcWdiy
    constructor: Plugin,
    @@ -33,7 +36,11 @@

    return this.each(function() {
    var $this = $(this);
    // Save the instance so it can be accessed later to use methods/properties etc
    // Create a new instance for each element in the matched jQuery set
    // Also save the instance so it can be accessed later to use methods/properties etc
    // e.g.
    // var instance = $('.element').data('plugin');
    // instance.someMethod();
    $this.data('plugin', new Plugin($this, options));
    });
    };
  3. @simonsmith simonsmith revised this gist Dec 4, 2013. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    !function(root, factory) {
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    @@ -17,7 +18,8 @@
    };

    Plugin.prototype = {
    constructor: Herotabs,
    // Reset constructor - http://goo.gl/EcWdiy
    constructor: Plugin,

    someMethod: function() {

    @@ -26,22 +28,26 @@

    // Create the jQuery plugin
    $.fn.plugin = function(options) {
    // Do a deep copy of the options - http://goo.gl/gOSSrg
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    var $this = $(this);
    // Save the instance so it can be accessed later to use methods/properties etc
    $this.data('plugin', new Plugin($this, options));
    });
    };

    // Expose defaults and Constructor
    // Expose defaults and Constructor (allowing overriding of prototype methods for example)
    $.fn.plugin.defaults = defaults;
    $.fn.plugin.Plugin = Plugin;
    });



    // Usage example
    require(['jquery', 'jquery.foo'], function($) {
    $('.test li').foo({
    require(['jquery', 'jquery.plugin'], function($) {
    $('.test li').plugin({
    test: 'option1',
    test2: 'option2'
    });
  4. @simonsmith simonsmith revised this gist Dec 4, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    !function(root, factory) {
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    @@ -12,8 +11,8 @@

    };

    var Plugin = function(container, options) {
    this.container = container;
    var Plugin = function(element, options) {
    this.element = element;
    this.options = options;
    };

  5. @simonsmith simonsmith revised this gist Dec 4, 2013. 1 changed file with 37 additions and 47 deletions.
    84 changes: 37 additions & 47 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,56 +1,46 @@
    !function(global) {

    !function(root, factory) {
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
    } else {
    factory(root.jQuery);
    }
    }(this, function($) {
    'use strict';

    var defaults = {
    'default': 'yeah',
    'test': 'simon'

    };

    // Wrapper function that allows us to pass it to define later
    var wrap = function($) {

    // Standard JS object stuff
    var Foo = function(element, options) {
    this.options = options;
    this.element = element;

    // Instance specific stuff
    };

    Foo.prototype = {
    // All your methods here
    };

    // Here is the actual jQuery plugin part. It just creates a new Foo object
    // for each matched element in the jQuery object.
    $.fn.foo = function(options) {
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    var $this = $(this);
    // Assign the instance to a data property so the methods can
    // be used
    $this.data('foo', new Foo($this, options));
    });
    };

    $.fn.herotabs.defaults = defaults;
    $.fn.herotabs.Foo = Foo;
    var Plugin = function(container, options) {
    this.container = container;
    this.options = options;
    };

    // Check for the presence of an AMD loader and if so pass the wrap function to define
    // We can safely assume 'jquery' is the module name as it is a named module already - http://goo.gl/PWyOV
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], wrap);
    } else {
    // Otherwise we assume jQuery was loaded the old fashioned way and just pass the jQuery object to wrap
    wrap(global.jQuery);

    Plugin.prototype = {
    constructor: Herotabs,

    someMethod: function() {

    }
    }
    }(this);



    // Usage

    // Create the jQuery plugin
    $.fn.plugin = function(options) {
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    var $this = $(this);
    $this.data('plugin', new Plugin($this, options));
    });
    };

    // Expose defaults and Constructor
    $.fn.plugin.defaults = defaults;
    $.fn.plugin.Plugin = Plugin;
    });

    // Usage example
    require(['jquery', 'jquery.foo'], function($) {
    $('.test li').foo({
    test: 'option1',
  6. Simon revised this gist Jun 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    !function(global) {
    'use strict';

    var defaults = {
    'default': 'yeah',
    @@ -45,7 +46,6 @@
    // Otherwise we assume jQuery was loaded the old fashioned way and just pass the jQuery object to wrap
    wrap(global.jQuery);
    }

    }(this);


  7. Simon revised this gist Jun 13, 2013. 1 changed file with 21 additions and 12 deletions.
    33 changes: 21 additions & 12 deletions amd-jquery-plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,40 @@
    !function(global) {

    var defaults = {
    'default': 'yeah',
    'test': 'simon'
    };

    // Wrapper function that allows us to pass it to define later
    var wrap = function($) {

    // Standard JS object stuff
    var Foo = function(element, options) {
    this.options = options;
    this.element = element;

    // Instance specific stuff
    };

    Foo.prototype = {

    // All your methods here
    };

    // Here is the actual jQuery plugin part. It just creates a new Foo object
    // for each matched element in the jQuery object.
    // for each matched element in the jQuery object.
    $.fn.foo = function(options) {
    options = $.extend({}, $.fn.foo.defaults, options);
    options = $.extend(true, {}, defaults, options);

    return this.each(function() {
    new Foo($(this), options);
    var $this = $(this);
    // Assign the instance to a data property so the methods can
    // be used
    $this.data('foo', new Foo($this, options));
    });
    };

    // Plugin defaults
    $.fn.foo.defaults = {
    'default': 'yeah',
    'test': 'simon'
    };

    $.fn.herotabs.defaults = defaults;
    $.fn.herotabs.Foo = Foo;
    };

    // Check for the presence of an AMD loader and if so pass the wrap function to define
    @@ -40,9 +48,10 @@

    }(this);

    // Usage

    require(['jquery', 'Foo'], function($) {

    // Usage
    require(['jquery', 'jquery.foo'], function($) {
    $('.test li').foo({
    test: 'option1',
    test2: 'option2'
  8. Simon renamed this gist Mar 5, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. Simon renamed this gist Dec 21, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. Simon created this gist Dec 21, 2012.
    50 changes: 50 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    !function(global) {

    // Wrapper function that allows us to pass it to define later
    var wrap = function($) {

    // Standard JS object stuff
    var Foo = function(element, options) {
    this.options = options;
    this.element = element;
    };

    Foo.prototype = {

    };

    // Here is the actual jQuery plugin part. It just creates a new Foo object
    // for each matched element in the jQuery object.
    $.fn.foo = function(options) {
    options = $.extend({}, $.fn.foo.defaults, options);
    return this.each(function() {
    new Foo($(this), options);
    });
    };

    // Plugin defaults
    $.fn.foo.defaults = {
    'default': 'yeah',
    'test': 'simon'
    };
    };

    // Check for the presence of an AMD loader and if so pass the wrap function to define
    // We can safely assume 'jquery' is the module name as it is a named module already - http://goo.gl/PWyOV
    if (typeof define === 'function' && define.amd) {
    define(['jquery'], wrap);
    } else {
    // Otherwise we assume jQuery was loaded the old fashioned way and just pass the jQuery object to wrap
    wrap(global.jQuery);
    }

    }(this);

    // Usage

    require(['jquery', 'Foo'], function($) {
    $('.test li').foo({
    test: 'option1',
    test2: 'option2'
    });
    });