Tiny, ambiguous Spry error in IE

Spry No Comments »

Just a little gotcha I came across at work yesterday, thought I'd post more as a reminder for me really.  We had a page using a Spry dataset to generate a sortable table and although it worked perfectly in Internet Explorer and Firefox, IE would show it's "loaded with errors" icon in the status bar.  As usual IE was helpful with it's error message "Error: Expected ':'". 

I assumed that might be because we weren't fully qualifying the Spry data within the Spry:Region, e.g. we were using {fieldName} instead of {dataSetName::fieldName}.  But that wasn't it. 

To cut a long and tedious story short, it was where we were passing a Spry binded field to a function - function({fieldName}).  As the argument was an integer I didn't think it need quotes (and as I said, it still worked in both browsers and IE was the only one to show an error) but when I added single quotes - function('{fieldName}') - IE stopped whining and all carried on working fine.

Not sure if that's a known thing or if it'll help anyone, but it annoyed me for a couple of hours.

Android/T-Mobile G1: First Impressions

Android 2 Comments »

So I've had my T-Mobile G1 for 3 days and I thought I'd post my first impressions. 

After putting in the sim card, battery then switching on you're immediately asked for your Google account details (or create a new account).  It takes a couple of minutes to link up to your account and sync the mail, contacts and calendar.  I loved that part, being back to the online synchronised stuff like when I had an HTC Wizard and an Exchange account.  But my first night playing left me thinking there's plenty of faults with it.  It's plasticy, the camera is pretty naff (especially compared to the N95's fairly decent one), the touch screen didn't seem very sensitive, there seems too be many interface devices (touch, trackball, keyboard, buttons) and more...

I was disappointed and was seriously considering sending it back.  But these all seemed to be hardware issues, Android, the software, is actually very nice. Okay, it doesn't have the "impress-your-mates-easily" multi-touch but the interface is pretty solid.  I like the notification "shutter" pull down, the scrolling physics when you flick lists is very "iPhone" and Android Market seems nice enough with some genuinely handy/clever apps. My first full day with it changed my opinion, I expect I was just getting used to it, but the touch screen felt more responsive.  T-Mobile's 3G service around London seemed to fairly robust too, better than my experience with o2's on my N95 in recent weeks. GPS is quick to lock and accurate. Even the multiple interfaces seemed to all seem to have a definite purpose. So it's still early days but it's definitely growing on me and I'm going to stick with it.

I have high hopes for Android in the future, as long as T-Mobile keep abreast of updates (I'm sure they will as it's in their and Google's interest to keep it progressing - and updates over-the-air is a nice touch).  Hopefully some of the popular iPhone apps are ported over (the train times one would be a nice start!), or maybe I should just learn a bit of Java and do my own...

And just to confirm Android has MMS, copy & paste and SMS forwarding, take that iPhone users! (I say in jest! I love the iPhone, I'm not one of these fanboy facists) :)

A little more Flexing

Flash , Flex No Comments »

As a little follow up to my post last week about starting to play with the Flex, I took my last.fm user application a little further to include more of the information they expose throught the audioscrobbler site.

Have a play: last.fm flex app v2

Spry Conditional Validation

Spry No Comments »

Spry blog posts seem a little thin on the ground, so I thought I'd start a series of Spry related posts covering some of the issues that we've come across in the rebuild of the HostelBookers site (yet to be launched).  Spry was the AJAX framework of choice for the project before I arrived at HostelBookers and as I'd had a little experience with it in my last job we decided to carry on with it.  I've since been named "SpryMan/Guy".  Not sure whether to be proud of that or not. Spry seemed to fit nearly all of our AJAX-type needs especially in the data display/filtering/sorting capacity, but it is lacking in some other areas, so we had to draft in jQuery for a nice datepicker and other UI bits and bobs.  I think if I got a chance to start from scratch I'd stick to one framework completely, and I think it would be jQuery (mainly for the size of the community).

So my first example will be on conditional validation.  We have a section of the site that has a little survey. The very first question decides whether you need to answer further questions or not.  If you get the futher questions displayed those fields need to be validated, if they're not, the validation is unnecessary. This will be a generic example, not quite as large as our actual page:

First you'll need to include the required Spry librarys and validation CSS in the <head> for the functionality we're going to use (if you haven't got Spry yet, it's available from Adobe Labs, but the download below includes all the files used in this example). For this example we'll use:

<link rel="stylesheet" type="text/css" href="spry/SpryValidationTextField.css">
<link rel="stylesheet" type="text/css" href="
spry/SpryValidationRadio.css">
<style type="text/css">
        .hide {display:none;}
</style>
<script type="text/javascript" src="spry/SpryUtils.js"></script>
<script type="text/javascript" src="
spry/SpryDOMUtils.js"></script>
<script type="text/javascript" src="
spry/SpryValidationTextField.js"></script>
<script type="text/javascript" src="
spry/SpryValidationRadio.js"></script>

The Spry CSS covers the validation indicators by hiding the messages by default and colouring fields when they meet certain criteria.  The ".hide" class is added to hide the optional questions in our form.  Of the Spry JavaScript, the first two are helper utilities, the second two are for the form input validation. We're only going to validate text fields and radio buttons in this example, but you can add select, textarea and/or checkbox valdiation as required.  I tend to use the minified versions of the JS files.  Packed ones are usually smaller, but the decompression of them can add an overhead.

Our XHTML form will be:

<form name="frmQuestions" id="frmQuestions" action="index.html" method="post">
<div id="valMoreQuestions">
<p>1. Would you like some more questions?</p>
<label for="moreQuestionsYes">
<input type="radio" name="moreQuestions" id="moreQuestionsYes" value="true" />
Yes please.
</label>
<br />
<label for="moreQuestionsNo">
<input type="radio" name="moreQuestions" id="moreQuestionsNo" value="false" />
No thanks.
</label>
<br />
<span class="radioRequiredMsg">Please select</span>
</
div>
 
<
div id="valExtraQuestion1" class="hide optionalQ">
<p>2. Extra question one.</p>
<label for="extraQuestion1">Who is your daddy?</label>
<input type="text" name="extraQuestion1" id="extraQuestion1" />
<br />
<span class="textfieldRequiredMsg">Please enter an answer for question 2.</span>
</
div>

<
div id="valExtraQuestion2" class="hide optionalQ">
<p>3. Extra question two.</p>
<label for="extraQuestion2">And what does he do?</label>
<input type="text" name="extraQuestion2" id="extraQuestion1" />
<br />
<span class="textfieldRequiredMsg">Please enter an answer for question 3.</span>
</
div>
<br /><br />
<input type="submit" name="submitAnswers" id="submitAnswers" value="Submit Answers" />
</form>

The form is pretty basic with a few things to note.  Each question is wrapped in a <div> with an id.  This is so Spry can reference <input> tag it contains and apply the appropriate validation. The <div> tags that wrap the optional questions are given 2 extra classes; "hide", to make sure they're not visible from the offset and "optionalQ" so we can easily select just the optional questions later.  Each question's <div> also includes a validation message within a nested <span> tag.  The class name of this determines which validation error has occured and so which message to display (these can be found in the Spry Documention, or customised with your own class names when constructing the validation object).    The <br />'s are just to make it a little tidier to view.

And finally out custom <script> to make it work:

<script type="text/javascript">
var valMoreQuestions = new Spry.Widget.ValidationRadio("valMoreQuestions", {validateOn:["change"]});
var valExtraQuestion1;
var valExtraQuestion2;
       
Spry.$$("#
valMoreQuestions input").addEventListener("click", toggleAdditonalQuestions, false);
       
function toggleAdditonalQuestions(e) {
if(e.currentTarget.value == "true") {
//loop over all elements with a class of "
optionalQ" and REMOVE the class of "hide"
Spry.$$(".optionalQ").forEach(function(qs){Spry.Utils.removeClassName(qs,"hide");})
//...and set up radio validation...
valExtraQuestion1 = new Spry.Widget.ValidationTextField("valExtraQuestion1", "none", {validateOn:["change"]});
valExtraQuestion2 = new Spry.Widget.ValidationTextField("valExtraQuestion2", "none", {validateOn:["change"]});
} else {
//loop over all elements with a class of "
optionalQ" and ADD a class of "hide"
Spry.$$(".optionalQ").forEach(function(qs){Spry.Utils.addClassName(qs,"hide");})
//destroy all the validation (if they were set up)
if(valExtraQuestion1) {
valExtraQuestion1.reset();
valExtraQuestion1.destroy();
}
if(valExtraQuestion2) {
valExtraQuestion2.reset();
valExtraQuestion2.destroy();
}
}   
}
</script>

So this JavaScript block is where all the magic happens.  First we set up our variables, "valMoreQuestions" can be set up with Spry Radio Validation straight away as it's always displayed. The next 2 are just set up as placeholders to be filled if they need to be shown. 

Next we add event listeners to the 2 radio buttons, so both fire the function "toggleAdditonalQuestions" when clicked. This is where the included SpryDOMUtils comes in very useful. "Spry.$$(CSS Selector)" allows you to select any elements from the DOM from their CSS selectors, so you can be as specific or generic as you need.  In the case of adding the event listeners to the radio buttons we've selected "#valMoreQuestions input", so all <input> tags under an element with the id of "valMoreQuestions".  This powerful little utility makes selecting and manipulating DOM objects a breeze.  In addtion to their selection we use the .addEventListener() method to attach the listener.

Now the event listeners are set up the last part is the function that called when these event are fired, "toggleAdditonalQuestions".  This function first tests to see that the value of the currentTarget of the fired event is true (the value of the radio button clicked).  If it is, we use another DOM selector to get all the elements with a class of "optionalQ".  On this selection of objects we call the forEach() method with fires a function for each element it's found in the selection phase.  Each object is passed to the function as an argument so we can then remove the class of "hide" for each one using the SpryUtils function "Spry.Utils.removeClassName(qs,"hide")" (qs being a single element found in the DOM selector).  This displays the 2 optional questions. We then add the validation to these options.

If the "No thanks" radio option is clicked the reverse happens.  The ".optionalQ" classed elements are selected again and have a class of hide added to them to make them disappear.  We then check if the 2 optional validation variable are set with an if statement (we do this becuase the user may have clicked "No thanks" first and the following functions wouldn't be available. If the variables are defined we use the reset() and destroy() methods on the validation object to remove it.

Spry automatically does the rest of the work to trigger the appropriate validation on clicking the submit button.  We also added the additional "validateOn:["change"]" to all the validation so they will also validation on the change event.

Download the example files. Also the Spry API is an invaluable reference for all the methods and options.

Forays into Flex

Flash , Flex No Comments »

Finally got around to installing Flex Builder 3 to have a play.  I've watched a few tutorials over the last few nights and thought tonight was about time to start coding.  Within 10 minutes I've got a very very basic application that will take your last.fm username, consume the audioscrobbler webservice feed of your top 50 albums and display them in a tile list. 10 minutes!  Why haven't I played with this before?

Anyway I've put my little app up (it's my first go, and does very little, but I'm chuffed with it).  Flex is definitely something I want to become proficient at!

Last.fm Top 50 Albums

Powered by Mango Blog. Design and Icons by N.Design Studio
RSS Feeds