Data-behavior is for JavaScript developers

Neeraj Singh

Neeraj Singh

October 10, 2012

I have written a lot of JavaScript code like this

1$(".product_pictures .actions .delete").on "click", ->
2  do_something_useful

The problem with above code is that class names in the html markup was meant for web design. By using css class for functional work, I have made both the design team and the front end development team perpetually terrified of making any change.

Class is meant for CSS

If designer wants to change markup from

1<div class="first actions">
2  xxx
3  <div></div>
4</div>

to

1<div class="first actions-items">
2  xxx
3  <div></div>
4</div>

they are not too sure what JavaScript code might break. So they work around it.

Same goes for JavaScript developers. They do not want to unintentionally remove a class element otherwise the web design might get messed up.

There has to be a better way which clearly separates the design elements from the functional elements.

data-behavior to the rescue

Nick Quaranto of 37Signals presented Code spelunking in the All New Basecamp in video.

In his presentation he mentioned data-behavior .

data-behavior usage can be best understood by an example.

1# no data-behavior
2$(".product_pictures .actions .delete").on("click", function(){});
1# code with data-behavior
2$('[data-behavior~=delete-product-picture]').on('click', function(){});
3
4# Another style with the same effect
5$(document).on('click',"[data-behavior~=delete-product-picture]",  function(){ });

The html markup will change from

1 <%= link_to '#', class: 'delete', "data-action-id" => picture.id do %>

to

1<%= link_to '#', class: 'delete', "data-action-id" => picture.id, 'data-behavior' => 'delete-product-picture' do %>

Above code would produce html looking something like this

1<a
2  class="delete"
3  data-action-id=""
4  data-behavior="delete-product-picture"
5  href="#"
6>
7  <button>Delete</button>
8</a>

Now in the above case the designer can change the css class as desired and it will have no impact on JavaScript functionality.

More usage of data-behavior

Based on this data-behavior approach I changed some part of a project I was working on to use data-behavior.

data-behavior is a very simple and effective tool to combat the problem of having clear separation between designer elements and JavaScription functional work.

Code snippet for reference

Over the period of time we have used this technique in many projects successfully. However sometimes I need to spend a while to find the right way to add data-behavior. I'm adding some code snippet so that I can find them here when I need them.

1%div{ class: "", "data-behavior" => "search-container" }
2
3.div{ data: { behavior: 'search-container' }, style: "" }
4
5= button_tag '×', :class => "", "data-behavior" => "search-container"
6
7= link_to 'Edit', "#",
8                  data:{ behavior: 'display-in-modal', url: '' },
9                  class: ""
10
11= f.check_box :include_annual_workplan,
12              'data-behavior' => 'input-include-workplan'
13
14= f.text_area :content, placeholder: "",
15                        class: '',
16                        data: { behavior: "comment-content" }
17
18= f.text_field :name, class: '',
19                      'data-behavior' => 'input-client-name'
20
21= form_for '', data: { remote: true, behavior: 'add-comment-form' } do |f|

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.