In part 1 we explored why exactly we may want to build our own jQuery plugin, so now in part two, we will cover exactly how to do so.
Note: these examples are through a bash command line, as I am fairly certain that is the most common way one interacts with Symfony. If you’re using Windows, you’ll get the gist of what to do, however.
Let’s start with initializing the plugin:
./symfony generate:plugin majaxJqueryPlugin
Now we will switch to our plugin directory:
We’re going to be providing web resources, so we will want to make web, js, and css directories. We will also make a temp directory to download files to:
Prepare a place to download what we need:
Here is our download and copy code. I have decided to host it from here, as jQuery UI doesn’t seem to provide direct download links.
Now for the fun part, to make it work. Since I like things to ‘just work’, here’s how I will do it. I will take a page from jQuery Reloaded and use a helper to actually load the files into the response, but instead of making people manually add it where they want it, we will just automatically shove it in if the plugin is enabled!
It’s time to make our helper directory:
Here is the contents of lib/helper/MajaxjQueryHelper.php:
The simplest way, hands down, is to simply include the sfJqueryReloadedPlugin in your project. This also enables you to include the sfAdminDashPlugin which makes for easy navigation and a nice login screen for your back-end systems.
However, the problem with jQuery Reloaded, is that it is both old, and for our purposes, incomplete.
Since the last update to jQuery Reloaded, jQuery has progressed to version 1.4.2 (from 1.3.2), and more importantly jQuery UI has upgraded to 1.8.2 (from 1.7.3) and added some very nice and easy pieces we can use in our quest to simplify some of the widgets users are commonly presented.
The other problem we will run into with jQuery Reloaded, is that it doesn’t provide the CSS half of the jQuery UI library, meaning we would not be able to see any widgets we used properly.
One of the biggest problems with any user interface is ensuring the forms and controls are simple and straight forward to use. While Symfony comes with a great many of widgets that you can use in your forms, some of the most common ones are not as user friendly as one would hope.