If you've used jQuery, or are just starting out, then you should realize jQuery functions as simplified namespace calls of otherwise tedious function calls. For example, $.ajax() is a jQuery function that provides almost all of the complexities of an ajax call into one name space where parameters are passed through in simple json style such as $.ajax({ url: 'http://spyk3lc.blogspot.com' });. You too can have this power!
To demonstrate this easy procedure, I'm going to take something simple, used quite frequently ... everywhere and make it into a simple, reusable set of functions. So what is this all powerful command? Simply tuning your cursor from default to waiting and back again.
First, I'll look at the most simple approach. Perhaps you simply want to set your cursor t wait while something is happening and then to default when that event is done. So at the start of your action you may have something like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.body.style.cursor = 'wait'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.body.style.cursor = 'default'; |
A prior warning, this next couple steps negates paying attention to jQuery name-spacing and is not "best practice", however, before I end this article, I will explain a better way to "crop" this down to size and review why name-spacing is important, I promise. For now, let's stick with the simple process of making those jQuery functions!
The first thing to know is that this doesn't have to be wrapped in your typical "document.ready" bubble, as jQuery already provides a "plug-in" style way of adding your functions, after you've called for jQuery. The mark up is simple:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Makes the initial jQuery ready call for ya and then proceeds | |
// to look at what you're wanting to do with the jQuery library | |
(function($) { | |
/* do work*/ | |
})(jQuery); // This closure ensure that your "plug-in" doesn't interfere with other plugins or libraries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!$.cursorDefault) { | |
// This if statement simply check the jQuery library ($) to if .cursorDefault is an already existing namespace. | |
} |
- Use a browser like Google chrome and go to your page or even the jQuery homepage
- Open the debugging console (ctrl+shift+j in Chrome)
- Click to type in the console
- Type $.YourDesiredFunctionName
- If it says something like undefined after you press enter, then you are in the clear to use this as a function name, just be careful it doesn't interfer with any other desired
Finally, we can make our first function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
if (!$.cursorDefault) { | |
$.extend({ // here is the "key" to it all. This command will "add" your function to the jQuery library | |
cursorDefault: function() { | |
document.body.style.cursor = 'default'; | |
} | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
if (!$.cursorDefault && !$.cursorWait) { | |
$.extend({ | |
cursorDefault: function() { | |
document.body.style.cursor = 'default'; | |
}, | |
cursorWait: function() { | |
document.body.style.cursor = 'wait'; | |
} | |
}); | |
}; | |
})(jQuery); |
Well, one function sets it to default or 0 as seen in the binary world, whereas the other provides it an action state. This is very similar to a boolean!
But how do we simulate this out of two functions? Parameters!
So now all we need is a new function name not being used by jQuery and one that probably want be used by other plugins. In our case we'll use "cursorWait", as we already have it tested and working, but this time we're going to make a parameter change to it.
So let's see how to rewrite this into one function name:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
if ( !$.cursorWait ) { | |
$.extend({ | |
cursorWait: function(bool_value) { // this boolean value can now be set to gives us more functionality to this call | |
switch (bool_value) { | |
case true: // if set to true then we do want it to make waiting icon | |
document.body.style.cursor = 'wait'; | |
break; | |
case false: // if set to false then we want it to have default style | |
document.body.style.cursor = 'default'; | |
break; | |
default: // despite the name of this case, we are going on relation to the function name, thus this will be the same as case true | |
document.body.style.cursor = 'wait'; | |
}; | |
} | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to make the cursor begin waiting | |
$.cursorWait(); | |
// and when you're ready to revert it to normal | |
$.cursorWait(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
if ( !$.cursorWait ) { | |
$.extend({ | |
cursorWait: function(bool_value) { | |
document.body.style.cursor = bool_value ? 'wait' : 'false'; | |
} | |
}); | |
}; | |
})(jQuery); |