Deferred object as callback

Posted October 10th, 2012 with 6 Comments

Callbacks are a nice way of Iversion of control, but are even more powerful with deferred objects.
Basically callbacks are a way to pass back the control from a called function, the deferred/promise just do the opposite: it returns the control the called function.

Consider this scenario: a table widget, with the classic delete/modify buttons next to each row. Since the table widget is a generic component, we want to handle the delete operation on the model outside the widget, and we don’t event want to extend the generic object to do that, we want to user a callback:

$.widget('my_table_widget',{
  onDelete: function(id_of_row) {
    var that = $(this);
    // call the backend and delete the record
    $.ajax({
      method: 'delete',
      url: '/api/records/'+id_of_row
      })
      .done(function() {
        // ok, record delete, now I should update the table
        that.widget('remove_row',id_of_row);
        });
      }
    });

In the example above: the callback first calls the backend to remove the record and then removes the row from the table (I mean the DOM). In order to do this: it needs to know something about the table widget (in this case, the method “remove_row”).
But what if we could pass the ball back to the widget? Let’s rewrite it with a deferred object:

$.widget('my_table_widget',{
  onDelete: function(id_of_row) {
    var deferred = $.Deferred();
    // call the backend and delete the record
    if (confirm('Delete?')) {
      $.ajax({
        method: 'delete',
        url: '/api/records/'+id_of_row
        })
        .done(function() {
          // ok, record delete, now I should update the table
          deferred.resolve();
          })
        .fail(function() {
          deferred.reject();
          })
      }
    else {
      deferred.reject();
      }
    return deferred.promise();
    }
  });

It’s more linear but, most important, the callback knows nothing about the remove_that_damn_row_from_the_dom method of the widget, it just passed back the control, it’s like saying “I’m done, it’s your turn”.
More separation, less documentation to read, easier to implement, less errors.

On the widget side, the callback should be treated this way

// somewhere inside the table widget, here is where we execute the callback
var callback_result = options.onDelete.call(widget,id);
// if the callback answer with a deferred/promise, this will
// handle it when it's resolved/rejected
if (isPromise(callback_result)) {
  callback_result
    .done(function() {
      // ok, asynch operation on the other side is over
      // remove the row from the dome
      widget.widget('remove_row',id);
      })
    .fail(function() {
      // do nothing
      })
  }

Event broadcast in jQuery

Posted June 27th, 2012 with 4 Comments

Eventbroadcast it’s a small library to improve the functionality of bind()/trigger() methods.
This plugin extends jQuery with the method $.trigger(‘‘) which calls the event named <event_name> on all elements previously registered to the event through .bind() and .one().

The main problem with the classic jQuery $(‘‘).trigger() method is that must be used on the element that receives the events, leaving Publish/Subscribe pattern uncomplete: we need to programmatically know the elements to cast the event to.

With the Publish/Subscribe pattern it’s easy to decouple the code in charge to create the event from the code that receives it: the event emitter doesn’t know (and doesn’t care), at runtime, who is listening to the event (speaking with the words of academics, it’s not aware of the topology of the elements).
The more the code is decoupled, the more is maintanable.

Continue Reading

jQuery Finite State Machine

Posted June 26th, 2012 with No Comments

jFsm stands for jQuery Finite State Machine and it’s a little script to help handling UI controls passing from a state to another.

An example

Have you ever found yourself in a situation writing code like this?

$('a.button1')
  .click(function() {
    // go to step2
    $('a.letsGoBack').show();
    $('a.button1').hide();
    $('a.button2').show();
    });  
$('a.button2')
  .click(function() {
    // go to step3
    $('a.letsGoBack').show();
    $('a.button1').hide();
    $('a.button2').hide();
    $('a.save').show();
    $('.controlToDisableInStep3').addClass('disabled');
    });

In this script some controls must be visible and enabled in a state (for example step #1 of a wizard) and/or invisible in the next state.
Nothing wrong with the code above, but as soon as the complexity of the UI grows up, the code become un-mantainable.

Wouldn’t be easier to define a set of possible states for our UI (for example step1, step2 and step3 for our multistep form) and define for each of them the behaviour of the UI components

$('a.button1')
  .jfsm('step1',{visible: true})
  .jfsm('step2',{visible: false})
  .jfsm('step3',{visible: false});
$('a.button2')
  .jfsm('step1',{visible: false})
  .jfsm('step2',{visible: true})
  .jfsm('step3',{visible: false});   
$('a.save')
  .jfsm('step1',{visible: false})
  .jfsm('step2',{visible: false})
  .jfsm('step3',{visible: true});
$('a.letsGoBack')
  .jfsm('step1',{visible: false})
  .jfsm('step2',{visible: true})
  .jfsm('step3',{visible: true});
$('.controlToDisableInStep3')
  .jfsm('step3',{className: 'disabled'});

Then with a simple command we change the state $.jfsm(‘step3′) and it will do the magic for you: .button2 and .button1 are hidden, .save and .letsGoBack are visible and the control .controlToDisableInStep3 gains the class ‘disabled’.
At first sight seems I’ve written more code, perhaps it’s true, but I’ve grouped this kind of logic in just one place and it’s easier to add element to the UI.

Syntax

$(‘element-selector’).jfsm(‘mystate’,state-descriptor): Define a descriptor for an element on the state ‘mystate’. The descriptor is an hash array which may contains one or more of these keys:

visible (true| false)
Make this element visibile or hidden in the defined state. The default is true (means that in any other state, if not specified, the element is visible)
className (string)
assign a CCS class to the element in the defined state
click (function)
assign a click handler for the element in the defined state (how did I do before this? Checking the state of the UI inside the click handler? Naaaa)
It’s possible to register your own key in the descriptor, see below.

$.jfsm(‘to-state’): Change the state the to ‘to-state’, triggering all the UI changes defined with the function above.
It always return a Deferred() object since the changes to the UI might be asynchronous.

$.jfsm('to-state')
  .done(function() {
    // ta da
    });

In addition, when a transition is completed, an event ‘jfsm-state’ is triggered on $(‘body’), the first argument is the new state name.

$.jfsm(new_descriptor,descriptor_handler,predefined_value): Define a new key for the state descriptor object

new_descriptor(string)
A string representing the key in the state descriptor
descriptor_handler(function)
The handler that operates the changes on the UI. The context (this) is the UI element to be changed, accepts two parameters f(from,to) which are the values of the key in the previous and new state, respectively (if these two values are the same in the transition, the handler will not be called).
See the example below about defining a new descriptor
predefined_value(anything|function)
Defines which is the default value of the field in the state descriptor

Adding a fadeout descriptor

Suppose I want to hide/show elements during transition using a fancy fade in/out effect.
First of all, register the *fade* key for the state descriptor:

$.jfsm(
  'fade',  
  function(from,to) {
    to = !!to; // to be sure is true or false
    if (to)
      $(this).fadeIn();
    else $(this).fadeOut();    
    },
  true
  );

Then define the behaviour of the button

$('a.button1')
  .jfsm('step1',{fade: true})
  .jfsm('step2',{fade: false});

And now $.jfsm(‘step1′) and $.jfsm(‘step2′) will switch the state between step1 and step2, happily fading in and out your button.
But we can do even better, jfsm handles correctly deferred objects, we can improve the handler in this way:

$.jfsm(
  'fade',
  function(from,to) {
    to = !!to; // to be sure is true or false
    var deferred = $.Deferred();
    if (to)
      $(this).fadeIn(function() { deferred.resolve(); });
    else $(this).fadeOut( function() { deferred.resolve(); });
    return deferred.promise();
    },
  true
  );

The event ‘jfsm-state’ will be triggerd only when the fade transition is completed, or I can just use the deferred object returned by $.jfsm:

$.jfsm('step1')
  .done(function() {
    // done fading
    });

 View the repository on GitHub

Free for personal and commercial use under the MIT/GPL license used by the jQuery core libraries.

  • Categories

  • Tags

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2 other subscribers

  • Follow me on Twitter