scriptNode

Web development with a focus on JavaScript RSS

Welcome to scriptNode: articles, tutorials and news about web development. Learn more…

JSQL: JavaScript Query Language

Advanced Matt Hackett Published March 22nd, 2009 by Matt Hackett

About JSQL

When I started my new job, I had to make the transition from YUI to jQuery. I'd seen some jQuery code before (in fact, its terseness and use of $ as both an object and a function were easy to spot), but I wasn't familiar with it. I actually thought that the query part of jQuery meant there was some kind of Standard Query Language interface. There isn't, but there is a really cool CSS3 selector instead.

Turns out, I was a little disappointed; I wanted to use something query-based. So, I started to throw this script together. I call it JSQL. Below are some quick examples of what it looks like in action:

JSQL always returns an object with an error property that's either Boolean false or a String stating what error occurred (for example, "Unknown command UPDAT3"). If it successfully retrieves or operates on elements, it returns a count:Number property with the number of elements and an elements:Array property with those elements.

Here are some interactive examples:

Example 1: Checkboxes

Option number 1
Option number 2
Option number 3

Example 2: Form Validation

First name:

Last name:

Email:

Example 3: Removing Elements

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

In the spirit of jQuery, I also stuck in a quick each method which you can use to iterate through the returned elements immediately instead of setting up your own loop. The context is the current element (like jQuery) and the return value is still the same:

Example 4: Selecting Elements

Give him that gun. Give him ALL the guns.

I started coding this several months ago, and just recently decided to revisit it. I found the code hard to read myself when making some modifications before publishing it here. That typically means it's poorly written. And I'm admittedly lousy at writing parsing code... so there's that. I also doubt it's realistic for any real-world websites (other libraries handle those jobs quite well), but I thought it would be fun to play with.

Some noticeable features lacking are equivalents to ALTER and INSERT INTO. I've also got it up on GitHub, so feel free to fork it, and maybe add those methods yourself!

Read other articles tagged: , , , ,

Spacius! Source Code

Advanced Matt Hackett Published March 9th, 2009 by Matt Hackett

I've been meaning to "release" this for quite some time now, but I wanted to do it in small doses, with in-depth explanations of the code. Making a game can be somewhat overwhelming, but it's just like any other large task: if you break it up into smaller pieces, it becomes much easier to manage.

But time is a commodity nowadays. I've got way less time to write, and when I do, I'd rather be writing code than tutorials (or honestly, playing video games). Sorry about that, but I hope the (well-commented!) source will help those of you out there who'd like to contribute to the slowly growing world of DHTML games.

You'll notice that this code is over 6 months old. And I'm the kind of developer who constantly updates his style, methods and patterns. Given that, there may very well be many mistakes or bad practices in here, some of which I may have improved upon by now, but others maybe not. Either way, feel free to comment on them, and I'll answer any questions as well.

Thanks for checking it out, and please be good to it. I'd love to know about any projects created with this source as a basis, and would appreciate links back here. Also please don't claim it as your own, that's not cool.

For now I am hosting the source code with GitHub.

Enjoy!

Read other articles tagged: , , , , ,

A Seemingly Snappier User Interface

Advanced Matt Hackett Published January 13th, 2009 by Matt Hackett

Say you've got a fancy user interface. You're leveraging AJAX well, you're making good use of best practices, and you've got fast servers that respond quickly. But your user actions still aren't quite as fast as you'd like. What else can you do?

If you've done things correctly, you've got a user interface that reacts positively to successful flows, but will also handle errors gracefully. Even though you're "doing it right," consider restructuring your application: assume success. Normally, your server is up and running and operations will be completely successfully. The edge case (hopefully) is the error case. Assuming this, we can create a slightly faster user interface.

Let's begin with a basic voting mechanism. You've got a control on your page that lets a user vote on an object, and a small bit of text to represent the state of said control. It may look something like this:

You haven't voted yet

This example is meant to be easy and functional; hopefully your controls are prettier. Regardless, here's a common flow for this request and its callback states:

  1. Listen for the click event on the control.
  2. Send the request.
  3. Display a "loading" message or a typical spinner image.
  4. If the call comes back successfully, provide "success" feedback to the user and update the control.
  5. If the call throws an error, provide an error message.

This flow is extremely common in today's web. But we want something more immediately responsive, so let's restructure the flow:

  1. Use onmousedown because it will fire off just slightly sooner than onclick would. (This might not suit all user interfaces but in this case it's fine, and it wins us a few milliseconds.)
  2. Provide "success" feedback to the user and update the control.
  3. Send the request.
  4. If the call throws an error, reset the controls and provide an error message.

This structure might seem a little off. Indeed, the error case even "lies" to the user for a few milliseconds if the request fails. But it covers all of its bases; none of the previous flow is omitted. Keep in mind that the error case is the edge case, and a very large majority of the time it's fine to assume success at first.

Using jQuery, here's what the code might look like:

And here's this flow working right here on the page:

You haven't voted yet
Read other articles tagged: , , , ,
© 2009 scriptNode