Git Product home page Git Product logo

jquery.nice's Introduction

jQuery.nice

jQuery helper for creating jQuery plugins.

Simple plugin

Create plugin core:

(function ($) {
        var NICE_PLUGIN = 'SayHello';

        if (!$.nice.createPlugin) {
            throw new Error('jQuery.nice.' + NICE_PLUGIN + ' require jQuery.nice core.');
        }

        var plugin = $.nice.createPlugin(NICE_PLUGIN, '0.1.0');
        plugin.options = { // default options

            textHello: 'Hello!'

        };

        $.fn.sayHello = function () {
            return $.nice.init(this, plugin, SayHello, arguments);
        };

        var SayHello = function (element, options) {
            element.text(options.textHello); // replace text for textHello from options
        };
})(jQuery);

Using:

// replace text on all .test-selector to "Hallo!"
$('.test-selector').sayHello();

// replace text on all .test-selector to "My Hallo!"
$('.test-selector').sayHello({
	textHello: "My Hallo!" // overwrite default options
});

// change default options
// "SayHello" is your nice plugin name
$.nice.getPlugin('SayHello').options.textHello = 'Hallo :)';

Events

How to create and call events.

  1. Add beforeChange asd afterChange to default options as names of your events.
  2. In your function add $.nice.call
(function ($) {
        //...
        var plugin = $.nice.createPlugin(NICE_PLUGIN, '0.1.0');
        plugin.options = { // default options

            textHello: 'Hello!',
            
            beforeChange: null,
            
            afterChange: null

        };

        $.fn.sayHello = function () {
            return $.nice.init(this, plugin, SayHello, arguments);
        };

        var SayHello = function (element, options) {
            $.nice.call(options.beforeChange); //call before callback
            
            element.text(options.textHello);
            
            $.nice.call(options.afterChange); //call after callback
        };
})(jQuery);

Using:

// replace text on all .test-selector to "My Hallo!"
// and apply callbacks
$('.test-selector').sayHello({
	textHello: "My Hallo!",
	beforeChange: function(){
	    alert('before');
	},
	afterChange: function(){
	    alert('after');
	}
});

Methods

How to create and call methods.

  1. Add method to your core function
  2. This methods will be accessed
  3. You can use more arguments
(function ($) {
        //...

        var SayHello = function (element, options) {
            var old_text = element.text();
        
            $.nice.call(options.beforeChange); //call before callback
            
            element.text(options.textHello);
            $.nice.call(options.change); //call change callback
            
            $.nice.call(options.afterChange); //call after callback
            
            this.changeTo = function(value){
                element.text(value);
                $.nice.call(options.change); //call change callback
            };
            this.revert = function(){
                element.text(old_text);
                $.nice.call(options.change); //call change callback
            };
        };
})(jQuery);

Using:

// replace text on all .test-selector to "My Hallo!"
// and apply callbacks
$('.test-selector').sayHello({
	textHello: "My Hallo!",
	change: function(){
	    alert('change');
	}
});

// for revert use
$('.test-selector').sayHello('revert')

// for apply your value
$('.test-selector').sayHello('changeTo', 'Hehe!');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.