Skip to main content

Unobtrusive JavaScript and the "lang" attribute

One issue that has come up in using Unobtrusive JavaScript is how do you assign instance specific data to the element.

For example, in the old days (pre UJ) you could iterate over a bunch of objects (each with it's own ID) and hard code the ID into the on{Event} attribute

i.e.
<% for item in items %>
<div onclick="someFunction(<%= item.id %>)">Click here to select item</div>
<% end %>

But in UJ, on{Event} attributes are no longer considered good practice. If only there was a way we could somehow encode the id with the HTML like we could in Flex (Flex supports dynamic attributes on objects).

You can assign a dynamic attribute using JavaScript, but putting a script tag into the loop seems very inelegant and against UJ principles. The other way would be to render all the objects in JavaScript first and then re-render them in HTML. This does seem rather wasteful in terms of processing however and full of index dependencies when you want to merge the JS objects with their HTML siblings (extra loops, etc...).

My latest solution (which may not be 100% semantic XHTML compliant) is to use the oft ignored and hardly used "lang" attribute to store the ID data.

<% for item in items %>
<div lang="<%= item.id %>">Click here to select item</div>
<% end %>

I then pick it up in jQuery using

$('div').click(function(){
clickHandler(this);
});

function clickHandler(element) {
var id = $(element).attr("lang");
// do AJAX call with the id
}

The lang attribute is supposed to let the browser know what language to use to display the content, but when you think about it, isn't the content written in the language itself? Not only that, but this is mostly only really used for the HTML element and only in very specific cases would you need it on a div (case in point bilingual pages).

Whilst I will admit this is not a failsafe solution, it does seem to meet my needs. I am wondering if anyone else has come across this problem and what their solutions would be.

UPDATE
I just thought of a couple of alternative methods, but none that I like as much as the one mentioned in the article.

1) Loop through the elements in JS only and create the HTML elements dynamically using DOM creation.

Disadvantage: DOM creation is expensive. It also creates elements which are impossible to debug the HTML when viewing the source directly (though you can in Firebug).

2) Use a compound id tag and parse out the irrelevant data (i.e. id="article<%= item.id %>").

Disadvantage: The id attribute must start with a non numeric character so parsing is a requirement. You cannot have an id like id="365". Parsing is expensive and error prone.

Comments

skuunk said…
I just thought of a couple of alternative methods, but none that I like as much as the one mentioned in the article.

1) Loop through the elements in JS only and create the HTML elements dynamically using DOM creation.

Disadvantage: DOM creation is expensive. It also creates elements which are impossible to debug the HTML when viewing the source directly (though you can in Firebug).

2) Use a compound id tag and parse out the irrelevant data (i.e. id="article<%= item.id %>").

Disadvantage: The id attribute must start with a non numeric character so parsing is a requirement. You cannot have an id like id="365". Parsing is expensive and error prone.

Popular posts from this blog

Freezing Gems

What is a gem and why would you want to freeze it?

In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author.

Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually.

Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy.

Freezing a gem is the process of transforming a gem into a plug in. Essen…

Comparing Rails' Active Record Pattern with Phoenix/Elixir/Ecto

Rails has a very well established Active Record pattern for dealing with the database. You have an Active Record model which maps to the database table, the schema of the model comes directly from the database schema and you place your model specific methods on the Active Record model. This file is also where you set your model relationships (e.g. has_many, has_one, belongs_to). Your instance of the model has all the methods built in.

In Ecto/Phoenix it's a little different. First of all, the database schema doesn't automatically map to the "model". In fact we don't really have models (as Elixir is a functional paradigm). What happens in one file in Rails, happens in essentially two (or more). You have a schema file (where you have to list out all the attributes and relationships). Using the schema file, your "instance" is essentially a data structure (with no methods on it). If you want to transform the data on your struct, you would use a context modu…

Unit/Functional Testing RubyAMF

One of my current projects is using RubyAMF to communicate with Flash (http://rubyforge.org/projects/rubyamf/). On the whole this is really nice because it allows you to transfer Ruby objects directly to ActionScript ones (as opposed to translating the object into XML, sending the XML and then recreating the object in ActionScript).
However, Rails does not provide a built in transport mechanism for AMF, so we cannot run functional testing directly on the data call (as we could for an XML or HTML transport layer). This is a show stopper for a lot of people (Rails w/o Unit testing = a big mess of trouble when something goes wrong).
We can though serve both the HTML and the AMF formats depending on the request format. This means that we can test the object instantiation logic and make sure there are no errors in the controllers (though we cannot check the actual format of the data being served). In the controller, instead of rendering AMF alone, do the following respond_to do |format|