Tyson Lloyd Cadenhead – User Interface Developer

April 1, 2011

Several months ago I wrote about event binding using ExtJS or Sencha touch. Something that you may or may not know is that you can achieve the same thing with jQuery using custom even binding.

In case you didn’t see my Sencha post about this, or if you don’t work with Sencha, let me tell you what I mean by event binding. Event binding can be used to loosely couple actions so they never break. Essentially, you broadcast to your entire application “Hey! I have something to say! Does anybody want to hear it?” Your modules can either say “Hey, I’d like to hear what you have to say” or else, they can just ignore it. Either way, nothing breaks. You can even have several modules respond to the same event in different ways.

So, on to the code… Make sure that you are using an up to date version of the jQuery library. At some point back in time, this didn’t work yet.

To create a custom event with jQuery, you can use the “trigger” function like this:


   $(document).trigger('my-custom-event', ['hi!  I am a custom variable!']);

You can actually bind a custom event to any DOM element, but for the sake of making it available to your entire application, the document object is usually a pretty safe choice. As you can see, we are also passing a variable.

Now you just need to have a listener at any place in your code that is executed before the trigger event, like this:


   $(document).bind('my-custom-event', function(v) {
      alert(v);
   });

So your code ends up looking something like this:


   $(document).bind('my-custom-event', function(v1, v2) {
      alert(v1);
   });

   $(document).bind('my-custom-event', function(v1, v2) {
      alert('firing ' + v2);
   });

   $(document).trigger('my-custom-event',  [
      'Hi!  I am a custom variable!',
      'Hi! I'm a different custom variable!'
    ]);

You should now see two separate alerts fired from the same trigger. Now all you have to do is bind custom events to your individual modules and they will never touch again. Feel powerful yet?

March 11, 2011

Having worked on several large scalable JavaScript and Ajax application over the past couple of years, I’ve started to form a definite opinion about what an application needs in order to be truly scalable and ultimately successful. So what does your application need?

Multiple Files that compress down
While you are developing and debugging your application, it is helpful to have many external JavaScript files. You wouldn’t try to make a large server-side project with just one file would you? So why would you have a JavaScript file that is hundreds if not thousands of lines long? Long files are harder to work with because you end up doing a lot of scrolling up and down the file rather than flipping between files. Of course, in the end, you will want a process that minifies, combines and compresses your files because they will load faster that way.

Namespacing
You don’t want a bunch of global variables and functions floating around waiting to be overwritten. I usually start by having an object name after the project and putting everything inside it in a logical way. It also is helpful to namespace your functions and variables because it makes it easier to literally log all of your variables with Firebug in order to see what is available. The idea of variable scopes is something that I’ve stolen from Coldfusion, because I’ve always loved the way that you can literally dump all of your variables. If you structure your JavaScript the same way, you can do the same thing.

Make Comments
Comments are helpful not only to help to debug code later on, but also to help you think through what the code is actually doing. I often write comments for my code before I write it in order to think through what I intend for it to do. Another great thing about comments is that you can use an application like JSDoc-Toolkit to extract the comments in the files into documentation.

Separate code into Model, View and Controller
Nothing makes for code that is hard to work with like mashing data, event listening and view logic together. I would recommend using some sort of templating system for your views. If you haven’t seen what they can do, it’s definitely worth checking out.

Use External JavaScript
JavaScript belongs in external files. When it’s external, you will have more control over the files for things like compression and documentation. The only exception to this should be cases where you have to get something from the server to pass into your JavaScript when the page loads. That’s it. The performance of external JavaScript is also better because it can be cached by the browser.

Don’t let your modules talk to each other
I try to break my code into small modules that absolutely never call each other. In practice, the modules should never even be aware of each others existence. That way if something changes in one module, it will never effect the other modules. When a module does make a change that everyone should be aware of, it announces it to the application. The modules that are listening for that particular event react to it. Again, modules should never be aware of each other.

These are a good starting point. What do you do to make your JavaScript applications scalable?

March 6, 2011

It’s no secret that web developers don’t lead a very active lifestyle. In fact, from the day I entered college until a couple of weeks ago, I had gained almost 25 pounds thanks to my lack of activity and disregard for the effects of food on my body.

However, a couple of weeks ago, I started to turn it all around.  Already, I’ve lost several pounds and I’m starting to feel healthier.  How could a web developer loose weight?  Ironically, the answer is technology.  I’ve been using a free iPhone app from loseit.com to track my calories.

I’ve tried other calorie counting programs like Spark People in the past, but I was never able to get past the poor user experience and confusing interfaces to actually start loosing weight.

Loose It, on the other hand has a very intuitive interface, both on the app and on their website.  It’s so easy that my mom can use it, and she does.  But it’s also an excellent tool for the tech-savy person whose idea of getting out in nature is moving their desk in front of the window.

What do you do to stay in shape in the world of technology?

February 23, 2011

I recently had the need to override all of my links on a Sencha Touch app with an onClick event. I needed to be able to open the links using the ChildBrowser plugin for PhoneGap. The problem was that I needed to grab the links before I rendered the content to the page and apply the onClick event instead of the href.

Essentially, I wanted to turn this:

<a href="myLink.html">My Link</a>

Into this:

<a onClick="myFunction('myLink.html')">My Link</a>

So how did I do it? I wrote a function to parse the string using Regex before I rendered it to the page. I thought that the whole operation my be fairly common, so I Googled how to do it, and to my surprise, there was literally nothing on the internet about how to do it, so I naturally thought it was worth posting.


// This is the function
var overrideLink = function(functionName, str){

        // Remove links with blank href tags
        // For exampe: <a href="">This would break everything!</a>
        str = str.replace(/href=""/g, '');

        // Replaces links with onclick events
        return str.replace(/href=\"(.+?)\"/, 'onclick="' + functionName + '(\'$1\');"');

};

var myString = 'This is a string that contains <a href="myLink.html">My Link</a> inside of it.';

// This is how you call it
overrideLink('myFunctionName', myString);

I’m still pretty new to Regex, so there may well be a better way to do this. If you know of one, don’t hesitate to put me in my place. Enjoy!

February 19, 2011

For some reason, I keep watching episodes of a television show called “Hoarders.” If you haven’t seen it, it’s about people who collect stuff. Collecting stuff is one thing, but these people have no organizational system for the items that they collect. They literally have piles of so much useless junk that their houses become essentially unlivable. Many of them don’t even have a single walkway to navigate through their homes. They could lead better lives, but they are unwilling to let go of their material possessions.

I think that is exactly what happens in technology when a good user interface goes bad. I know that there are user interfaces that are legitimately bad to start with, but that’s beyond the point of this discussion.

As designers and developers, we make interfaces that provide wonderful user experiences. After which our clients and sometimes even end-users will come back with items that simply have to be added to the interface.

Sometimes the additions really are necessary and sometimes they aren’t. For the sake of discussion let’s say that the additions actually are necessary.

What happens when you have an interface that doubles or triples in visual complexity? Practically speaking, it will make the user lose focus. It will make them unsure of what they are supposed to do and how they are supposed to do it. Simple tasks will take them longer. In the worst case, they will give up on your interface and move on to something else.

The people creating the software or making the decisions about the software may feel like everything they are adding is needed. However, in the end, you have to let some things go. Otherwise you are just another user interface hoarder.

Here are some valuable questions to ask if you’re planning to add something to an existing interface:

  • Is this really needed?
    Some additions are important and others are not. Something that is flashy or “cool” may not necessarily be something that will make life easier or better for your user.
  • Will this improve the user experience?
    In many cases, the client is going to want to get as much information about the user as possible. Information is valuable. The more we know about our users, the more we can improve their experience in the long run. The problem comes when asking questions to the user gets in the way of their current experience. If you can, find ways to get to know the user without making them fill our lengthy forms. Nobody likes filling out forms. Forms are even worse on mobile devices without an external keyboard. If you want your user to submit a form, a basic rule is to make it short.
  • Does everything need to be shown at once?
    If you determine that everything you’re adding is important, perhaps you can make the interface a little easier by not showing the user everything at once. Forms are often easier to complete in short steps where the user can focus on one question at a time. This also makes it easier for the user to react to form validation, because the problematic fields will be fresh in their minds.
  • Emphasize the things that are of the highest import
    I’m not here to give a lesson on graphic design, but if something is important, like a call to action, it should stand out. The user needs to be drawn to it. Once you start emphasizing what is extremely important, you may find that some of the less important things can be cut.

Nobody is going to leave you interface or website because it is too simple. However, if your interface is too hard too use, it will probably only take a few short seconds for your users to leave and never return. If you want to give your users a rewarding experience with your application, you just may have to clean your house.