jquery plugin development

27
JQuery Plugin Development Faruk Hossen

Upload: faruk-hossen

Post on 07-Aug-2015

131 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Jquery plugin development

JQuery Plugin Development

Faruk Hossen

Page 2: Jquery plugin development

What is JQuery

➢ JQuery is a Javascript Library.➢ The jQuery library contains the following features:

○ HTML/DOM manipulation○ CSS manipulation○ HTML event methods○ Effects and animations○ AJAX○ Utilities

Page 3: Jquery plugin development

➢ jQuery is a javascript object. var jQuery = function( ) { //object properties go here.

};

JQuery Object

Page 4: Jquery plugin development

➢ ‘$’ is shorthand for jQuery➢ When we call the $() function and pass a selector to it,

we create a new object$( document );

$(“div.menu”);

$(“div.menu”).click(function(){$(this).find(“ul”).show();

}

JQuery Object

Page 5: Jquery plugin development

➢ Javascript prototype object is ■ $.fn or ■ jQuery.fn

JQuery Prototype Object

Page 6: Jquery plugin development

➢ All objects have a prototype property. ➢ The JavaScript prototype property also allows you to add new

methods to an existing prototype.➢ It is simply an object from which other objects can inherit

properties

What is prototype of an object in js

Page 7: Jquery plugin development

function person(first, last, age, eye) {this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eye;

}person.prototype.name = function() {

return this.firstName + " " + this.lastName};

var myFather = new person("John", "Doe", 50, "blue");console.log(myFather.name());

What is prototype of an object in js

Page 8: Jquery plugin development

function Person(name) { this.name = name;}Person.prototype.sayHello = function () { alert(this.name + " says hello");};

var james = new Person("James");james.sayHello(); // Alerts "James says hello"

What is prototype of an object in js

Page 9: Jquery plugin development

jQuery.fn = jQuery.prototype.

from jquery source code:

jQuery.fn = jQuery.prototype = { init: function( selector, context ) { // ... return this; } // jQuery API methods }

What is $.fn

Page 10: Jquery plugin development

➢ In object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object

➢ There are really two main contexts of 'this' in jQuery. ○ ‘this’ first refers to a DOM element ○ ‘this’ refers to a jQuery object.

What is ‘this’

Page 11: Jquery plugin development

$( "p" ).click(function() { $( this ).slideUp();//is it wrong? this.slideUp() //is it wrong?});

What is ‘this’ (continues…)

Page 12: Jquery plugin development

jQuery.fn.newTarget = function() {this.hide(); //will it show error?$(this).hide(); //will it show error?

}$(‘p’).newTarget();

What is ‘this’ (continues…)

Page 13: Jquery plugin development

What is JQuery Plugin➢ A jQuery plugin is simply a new method that we use to extend jQuery's

prototype object. ➢ By extending the prototype object you enable all jQuery objects to inherit

any methods that you add. ➢ As established, whenever you call jQuery() you're creating a new jQuery

object, with all of jQuery's methods inherited.

Page 14: Jquery plugin development

➢ Portability➢ Time

Why JQuery Plugin

Page 15: Jquery plugin development

➢ Packaging a common function within a jQuery plugin will normally make it more portable.

➢ Doing so allows you to easily integrate the plugin into any number of projects within a short amount of time.

➢ Reusability, another buzz word, comes hand in hand with this.

portability

Page 16: Jquery plugin development

➢ It will save you (and your users) time. ➢ The time it takes to develop a robust plugin is clearly worth it

when you consider what it enables.

Time

Page 17: Jquery plugin development

➢ jQuery allows methods to be added to its library. ➢ When called, these methods are passed the jQuery object within

the JavaScript ‘this’ object.➢ The DOM nodes can be manipulated as required ➢ The method should return ‘this’ so other functions can be

chained.

How JQuery Plugin Works

Page 18: Jquery plugin development

1.plugin function2.options 3.callback4.ability to chain.

JQuery Plugin Components

Page 19: Jquery plugin development

$( "#clickme" ).click(function() { $( "#book" ).animate({ opacity: 0.25, left: "+=50", height: "toggle" }, 5000, function() { // Animation complete. });

});

JQuery Plugin Components

Page 20: Jquery plugin development

(function($) {jQuery.fn.hightlight = function(options) { ... };

})(jQuery);

(function($) {$.fn.hightlight = function(options) { ... };

})(jQuery);

Note: Using ‘jQuery’ rather than ‘$’ ensures there are no conflicts with other JavaScript libraries.

JQuery Plugin Declaration

Page 21: Jquery plugin development

//default settings(function($) {

$.fn.highlight = function(options) { var settings = {‘highlight-color’:’white’,’highlight-

background’:’yellow’};

$.extend(settings,options);

};})(jQuery);

Options in Plugin

Page 22: Jquery plugin development

1. $(“p”).highlight(); //uses default settings.

2. $(“p”).highlight(//uses color:white,background:yellow‘color’:’white’; );

3. $(“p”).highlight(//uses color:white,background:red‘color’:’white’‘background’:’red’; );

Passing Options

Page 23: Jquery plugin development

(function($) {$.fn.highlight = function(options,callback) {

var settings = {‘highlight-color’:’white’,’highlight-background’:’yellow’};

$.extend(settings,options);

callback.call(this); };

})(jQuery);

Callback function

Page 24: Jquery plugin development

1. $(“p”).highlight(//uses color:white,background:red{ ‘color’:’white’ ‘background’:’red’},function(){ //execute after complete} );

Callback function

Page 25: Jquery plugin development

(function($) {$.fn.highlight = function(options,callback) {

var settings = {‘highlight-color’:’white’,’highlight-background’:’yellow’};

$.extend(settings,options);

callback.call(this);return this; // it is for chaining };

})(jQuery);

Ability to chain

Page 26: Jquery plugin development

Ability to chain

Page 27: Jquery plugin development

THANKS