How I learnt about jQuery Deferred thanks to Rails

Jan Dudulski

How I learnt about jQuery Deferred thanks to Rails

If you are not a regular JS developer but Ruby one or whatever, you probably don't know every feature of JavaScript or jQuery, even if you use them every day. But some of them are worth spending some time to get to know - for me one of such features was jQuery.Deffered which I would like to introduce in this post.

The context

I love the idea behind unobtrusive javascript, silently promoted with the release of Rails 3. It helps to keep the code clean, separate concerns of data and logic like controllers and views.

In one of our projects I had to replace the standard browser confirmation prompt with a  fancy one and I was wondering if I can just re-use the Rails unobtrusive solution with data-confirm param. Then, the problem occured.

The problem

Rails wisely assigns built-in confirm method to the $.rails.confirm, which allows developer to simply replace it with anything (s)he wants. The problem is, that you cannot provide any method which would open up a prompt, wait for click and return the boolean value as confirm does. No wait here, sorry. The problem is well known, but there is no simple solution yet.

O'rly?

Eight months ago (sic!) Mr akzhan proposed the solution - he rewrote jquery_ujs confirm to work with $.Deferred. After polishing, his work landed couple of days ago in assync-confirm branch of jquery-ujs maintainer repo. Unfortunatelly, Steve will not merge it with master untill it is used by more people at production. However, if you like, you can just download the fork, put it somewhere into your assets and require the standard jquery_ujs instead.

Here's a simple code which works for me (with jConfirm and coffee-script):

$.rails.confirm = (message) ->
  answer = $.Deferred()
  jConfirm message, 'Please confirm', (result) ->
    if result
      answer.resolve()
    else
      answer.reject()
  return answer.promise()

And that's all! Dead simple, isn't it? It will work with standard data-confirm, e.g.:

link_to some_path,
  { :method => 'delete', :remote => true, :confirm => "o'rly?" }

How it works?

So, you can ask - what is this magic $.Deffered? In the simplest words it's an object with stack of callbacks inside. You can add a new one from the outside, and resolve/reject the object which would end up with specific callbacks run. There are three stacks - one is called when an object receives resolve() call, another one when it receives reject() call, and lastly a one when it receives any of the two calls.

In the example above we're creating a deffered object which would get resolve/reject call from the jConfirm callback, depending on user decision. Without waiting for response, the $.rails.confirm will return the misterious promise object - it's a deffered object without resolve/reject methods. You can still add new callbacks, but you cannot decide when to run them. We left that decision to jConfirm callback.

It's also worth knowing that you cannot set a state of deffered object twice. However if you add a callback after resolving/rejecting, it would be fired up instantly, depending on the state of course.

Conclusion

Read the whole doc at jquery.com and just use it when you need the ability to postpone adding your callbacks. There are some objects already deffered - like $.ajax. You don't need to specify callbacks inside constructor, you can move the object around and add callback anytime you need with then, done, fail or always.

Disclaimer

If you decide to use assync-confirm version of jquery-ujs, make sure that a little bug is fixed, or fix it yourself.