Since we’ve started the conversation, let’s take a step back and talk about something a little less fun, but just as important: when is it okay to require JavaScript use?
My hard and fast rule is that anywhere a login is required to reach, you can require JavaScript. If it’s publicly accessible, however, you should do everything possible to avoid requiring JavaScript. It takes more work to have a JavaScript and non-JavaScript version, but it makes sure your site remains accessible.
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:
cd plugins/majaxJqueryPlugin
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:
mkdir web
mkdir web/js
mkdir web/css
mkdir temp
Prepare a place to download what we need:
cd temp
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:
mkdir lib/helper
Here is the contents of lib/helper/MajaxjQueryHelper.php:
For the last little trick before we enable the plugin, open up config/majaxJqueryPluginConfiguration.class.php and add the following code to the initialize() function:
You’re done! Now you just have to go back to your project root, and edit your project configuration to add the plugin, and publish it’s assets.
Here is the line to add to your ProjectConfiguration’s setup() function, just in case you need it:
$this->enablePlugins('majaxJqueryPlugin');
To publish it’s assets:
./symfony plugin:publish
And now you’re ready to use jQuery throughout your Symfony project!
To download a copy of the majaxJqueryPlugin I have made (and save yourself some work!), use the link below, and you just have to follow the last step to enable the plugin in your project:
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.
Just a quick bit that I’m sure I will revisit later, but that I wanted to get out there sooner…
When embedded forms are saved, if you want to hook into the form save for the embedded form, use the saveEmbededForms function as save or doSave is never called on the embedded form.
The embedded form has it’s saveEmbeddedForms function called, and then $form->getObject()->save().
I need to get in touch with the Symfony devs and see if we can’t get some clarification on this setup in 2.0, as this “smells” wrong, but maybe it is just me.
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.
Over the next week or two I will be releasing (and posting about) a number of symfony widgets that I’ve powered up with a little (or in some cases, a lot) of javascript. The first step, though, will be a plugin that adds jQuery and jQuery UI into your Symfony stack.