This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/Pwyxbw
1) The first step would be putting all the existing jQuery slideshow code into a plugin scope.
$.fn.makeSlideshow = function(options) {
// get all images
var images = $(this).find('li');
// existing slideshow code.
// jQuery plugin’s practice: return this.
return this;
};
Then we call our plugin by the following code:
$('#slideshow').makeSlideshow();
2) Assuming now we want to add additional options. For instance, allowing user to customize theme option and slideshow interval.
$.fn.makeSlideshow = function(options) {
// default settings
var settings = $.extend({
theme: 'theme-a',
interval: 3000,
}, options );
// apply theme
$(this)
.removeClass().addClass('slideshow').addClass(settings.theme);
setInterval(nextSlide, settings.interval);
// existing slideshow code goes here.
};
We can now call our slideshow plugin with options.
$('#slideshow').makeSlideshow({
theme: 'theme-b',
interval: 1000
});
You may find the final code example in the following URL. It also includes ‘theme-a’ and ‘theme-b’ defined in the CSS style.
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/Pwyxbw
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/Pwyxbw
1) The first step would be putting all the existing jQuery slideshow code into a plugin scope.
$.fn.makeSlideshow = function(options) {
// get all images
var images = $(this).find('li');
// existing slideshow code.
// jQuery plugin’s practice: return this.
return this;
};
Then we call our plugin by the following code:
$('#slideshow').makeSlideshow();
2) Assuming now we want to add additional options. For instance, allowing user to customize theme option and slideshow interval.
$.fn.makeSlideshow = function(options) {
// default settings
var settings = $.extend({
theme: 'theme-a',
interval: 3000,
}, options );
// apply theme
$(this)
.removeClass().addClass('slideshow').addClass(settings.theme);
setInterval(nextSlide, settings.interval);
// existing slideshow code goes here.
};
We can now call our slideshow plugin with options.
$('#slideshow').makeSlideshow({
theme: 'theme-b',
interval: 1000
});
You may find the final code example in the following URL. It also includes ‘theme-a’ and ‘theme-b’ defined in the CSS style.
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak
This is lecture 5 of Beginning jQuery course. In this lecture, we explore how to use jQuery plugin.
The jQuery’s learning center provides a solid tutorial on creating plugin:
https://learn.jquery.com/plugins/basic-plugin-creation/
Here are the key points for a plugin:
Register a plugin:
$.fn.pluginName = function() { // $(this) refers to the selected elements return this; };
Plugin may take simple parameters:
$.fn.pluginName = function(param1, param2) { // your code goes here. return this; };
Plugin may take options as an object:
$.fn.pluginName = function(options) { // default settings var settings = $.extend({ foo: ‘bar', foo2: 123, }, options ); // your code goes here. return this; };
When to use options object?
We can accept options as an object instead of taking comma-separated parameters. This is usually a more flexible way. It allows accepting many options without certain order. It also allow skipping options to use default plugin’s option value.
We will use our slideshow code in our previous lab to demonstrate the plugin creation. The original code was in the following URL:
http://codepen.io/makzan/pen/RPbzKz
Here is a code demonstrating how we use sortable in real-world project. In this example code, which I extracted from my client project, the server-side renders a list of content and lets client-side re-ordering the list. The new order is then sent to server to save into database by using jQuery’s ajax post.
<div id='sortable'>
<%- @notes.order('rank').each do |note| %>
<div class='note' data-note-id='<%= note.id %>'>
<div class='dragging-area'>☰</div>
<%= note.content %>
</div>
<%- end %>
</div>
And the JavaScript that enable the sorting and posts new orders to server:
$('#sortable').sortable({
handle: '.dragging-area',
update: function( event, ui ) {
var array = [];
$('#sortable').children().each(function(){
array.push($(this).data('noteId'));
});
$.post('/notes/reorder', {new_order:array}, function(data){
// server-side handles the new order and update the order rank in database.
console.log(data);
});
},
});
Please note that the code contains server-side script (Ruby) and it probably doesn’t work in your browser without a server running. The purpose of the code is to demonstrate how it works.
I created a course dedicated for using jQuery mobile to create mobile web application which is free for online reading. If you need to to create mobile web application, this is a good starting point.
Here I list some 3rd party jQuery plugins that I find useful when building websites.
We have learned a lot in this Beginning jQuery course.
getJSON
method.–Thomas Mak