The right way to add AJAX support to your site Written on May 22, 2011, by .

It seems that every website now has at least some AJAX component to it. Whether it is an updating Twitter feed, checking if a username is available or a full-blown web app, they all have one thing in common – A connection back to a server via an external API. It might return be a single letter or a 500k XML file but you have a defined way to request and retrieve your data.

But what if that external API gives different results than when you load the page? This is usually a case of duplicated code where only one copy gets changed. We want to make sure that someone loading the page and someone getting an AJAX update always get the exact same data at a given point in time.

There is the dumb solution that I have written about before – not outputting it at all and doing an immediate AJAX update when the page first loads. This post is about a much more correct solution.

The most sensible way is to generate that data via a single function and use it in both places. You want to be defining an internal API where your main site display code and AJAX code are both consumers of it. By doing this you also make sure that your business logic is neatly partitioned away and any changes to it are then reflected throughout. People who use MVC frameworks already do this as a matter of course and their views (display logic) are kept well apart from the models (business logic).
Read more

Read more from the Coding, Uncategorized, Webdev category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

The evils of premature optimisation Written on March 23, 2011, by .

A pimped car
Image via Jens Christoffersen on Flickr

It is always tempting to try and speed up the bit of code you just wrote. Sometimes it is tempting to try and speed up code you haven’t even started yet by over-thinking the algorithm.

Unless your codebase is functionally complete or the code is so slow and/or resource hungry that it stops things from working, it is too early to optimise it beyond what is absolutely necessary.
Read more

Read more from the Coding, Misc category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

jQuery, confusing sample code and elegance Written on March 13, 2011, by .

Picture of JavaScript Code
Image via Andrew Dupont on flickr

Have you ever found a cool snippet of code that does almost what you need, tried to tweak it but it is formatted in such a way that you can’t work out what the hell it is doing?

I’ve noticed this so often recently I thought I’d try and steer people back onto the sanity bandwagon.

Part of writing elegant code is to make it both understandable and easier to debug, re-use and maintain.

The following is a JavaScript example although it can effect any language.
Read more

Read more from the Coding, JavaScript, jQuery, Rant, Webdev category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

XMLHttpRequest Misuse on Page Load with AJAX Written on February 27, 2011, by .

Introduction

XMLHttpRequest is a JavaScript function that allows the core dynamic data part of AJAX (Asynchronous JavaScript And XML) to work. It lets JavaScript running in a web browser call a URL on the same server and receive data back again to process. Either normal HTML, XML or any format such as JSON can be returned and the JavaScript can do what it likes with it. This lets the page update itself or submit a form without making the whole page refresh which typically gives the user a much better experience. Obvious examples are a news website with up-to-date news ‘ticker’-style headlines or live sports commentary, Viewing more comments in YouTube, Search engine pop-up suggestions as you type, and so on.

Read more

Read more from the Coding, Rant, Webdev category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

#include “blog” Written on February 6, 2011, by .

I thought it was about time I started a real blog and so here we are.

I do write on blogs for two projects I work on, however both are fairly specific so I wanted one I can ramble on about anything I am interested in or up to. There theme of the blog is mostly programming, technology and general geekery. I’ll list each of my interests below. there is always a possibility that I might do an out-of-theme post once in a while -

Read more

Read more from the Misc category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

© Copyright etrium (Dan's stuff) - Powered by Wordpress - Theme based on The Azure Hut Designed by Speckyboy.