Tuesday, September 10, 2013

jQuery deferred resolution and rejection based on boolean value

Do you hate writing this as much as I do?

    var dfd = $.Deferred();
    if (something) {
        dfd.resolve();
    } else {
        dfd.reject();
    }

Well no more!  Enter "cond".

$.Deferred = (function (baseDeferred) {
  var slicer = [].slice;
 
  var cond = function (truthy) {
    return this[truthy ? 'resolve' : 'reject'].apply(this, slicer.call(arguments, 1));
  };
 
  return function() {
    var dfd = baseDeferred.apply(this, arguments);
    dfd.cond = cond;
    return dfd;
  };
 
})($.Deferred);

Unfortunately, as shown by the relative complexity of this code, jQuery doesn't actually use a prototype for the deferred objects it creates, so you have to 'duck punch' the feature in.

Usage (after including the previous script somewhere):

var dfd = $.Deferred();

dfd.cond(true, { some: 'data' });

dfd.then(function (res) {
    console.log(res);
});

Sunday, August 18, 2013

How to create your own EventEmitter in NodeJS in 3 Steps

Note: This post does not seek to explain what the EventEmitter is.  For that, look at Node's site.  I'm going to show you how to make your custom object into one.  This is handy for many reasons.  I recently created a node module which wraps grunt and exposes the output from it as an EventEmitter.


Step 1: Bring in the modules


// events contains the EventEmitter 
// class we will inherit from
var events = require('events');
// and util contains `inherit` which 
// we will use to set up our object's inheritance
var util = require('util');

Step 2: Construct your class, followed by the EventEmitter's

You will want to call the EventEmitter's constructor at the end of your own.  It's standard practice when inheriting from another class that you call into the parent's constructor in order to preserve any functionality within it.  I've excluded it before, and it's not the end of the world, it just sets some default properties.  You can see what the constructor in Node does on Github.

var MyClass = function () {
    // Your class' constructor magic

    // Call the EventEmitter constructor
    events.EventEmitter.call(this);
}

Step 3: Set up the prototypal inheritance

util.inherits(MyClass, events.EventEmitter);

Well that was easy.  What this does is set up the prototype chain for us.  You can read more about the util.inherits again on Node's website.

The Final Code

var util = require("util");
var events = require("events");

function MyClass() {
    // Your class' constructor magic

    events.EventEmitter.call(this);
}

util.inherits(MyClass, events.EventEmitter);

MyClass.prototype.emitTest = function(data) {
    this.emit("data", data);
}

// Consumption

var instance = new MyClass();
instance.on('data', function (msg) {
    console.log(msg);
});

instance.emitTest("Hello World"); //=> Hello World

Tuesday, April 23, 2013

Express REST Controller - Rapid REST APIs

Ever wanted to create a RESTful interface on your Node/Express server that exposes an in-memory Backbone Collection with one line of code?

Okay, so that's probably just me but let me know what you think of this:

Express REST Controller on GitHub

It can be used to create multiple REST APIs on a Backbone Collection like so:

var app = express();
var customerController = new Controller().bind(app, 'customer');
var orderController = new Controller().bind(app, 'order');
var itemController = new Controller().bind(app, 'item');

After executing these lines you can now hit your Node/Express server with full CRUDL at /customer, /order and /item.

I think it's cool.

Monday, April 01, 2013

Making your Backbone objects into promises

So awesome.

I used to return back promises from my views, for example.  Now I just make my entire view implement the Promise API.  In this fashion, whomever creates the view can wait for a result (think modal popup with the possibility of multiple pages of user data being returned back to the base page).


    var MyChildView = Backbone.View.extend({
       dfd: null,

       initialize: function () {
           this.dfd = $.Deferred();
           this.dfd.promise(this); // Make the view a promise

           this.dfd.always(this.remove); // Self cleanup anyone?
       }

       //Later
       method: function () {
           this.dfd.resolve({ foo: 'bar' });
       }
    });

This enables now something like the following.
    var MyParentView = Backbone.View.extend({
       render: function () {
           var childview = new MyChildView();
           childView.then(this.doSomethingElse); // Do something meaningful with the promise
       }
    });
This approach is clearly superior than events or success callbacks when your parent is waiting on the result of its child. It also allows your views to become parent of entire asynchronous workflows orchestrated with promises.

Presentation on Backbone application organization

I recently gave a few talks (Thomson Reuters, Best Buy and at JS MN) about how Backbone JS applications should be organized.  It speaks to common errors, bug-prone approaches, best practices, and just plain general organization.

It's filled with dogma that's meant to set a team straight when it comes to working together and preventing the evil spaghetti monster from creeping into your app.

I'll break it down later and hopefully provide some annotated sample files, but until then here it is.

My presentation on Backbone JS application organization

Tuesday, January 22, 2013

I $.Promise - Part 3 - How to Make Your Own jQuery Promises

In Part 1 and Part 2 I talked about why promises exist and what you can do with them, but the true power of Promises cannot be realized until you start creating them yourself.  They're especially handy for points of user interactions, animations and blended AJAX operations.

The basic form

The basic form follows four basic steps when creating your own promise/Deferred implementation:
  1. Create the Deferred object.  I use the word Deferred here on purpose as this object should be retained internal to your function or object only, it should never be passed to the consumer.  In this way, no one can muck with the resolution or rejection of your promise and keeps your separations clear.
  2. Do some async stuff.  Whether it's an AJAX call, animating or getting feedback from the user.
  3. Resolve or Reject your Deferred.  At some point this thing needs a result.  Your method controls when and with what it's resolved or rejected.
  4. Return a Promise.  A promise is a deferred without the capabilities of resolving or rejecting.
// Totally contrived example of showing an edit form in a modal in Backbone.
function modalEdit(model) {
    // 1. Create the Deferred object.
    var editing = $.Deferred();

    // 2. Do some async stuff.
    var editView = new ModalEditView({ model: model, el: $('#modalSpot') });
    editView.render();

    // 3. Resolve or reject your deferred.
    model.on('sync', function () {
        editing.resolve(modal); // Resolve on save.
    });
    editView.on('cancel', function () {
        editing.reject(); // Reject on dialog cancel.
    });

    // 4. Return a Promise.
    return editing.promise();    
}


Your core Deferred object control functions are:

Deferred.resolve(args) - Executes 'done' handlers, passing args.
Deferred.reject(args) - Executes 'fail' handlers, passing args.
Deferred.notify(args) - Executes 'notify' handlers, passing args.

That's really all you need to get going.  If you'd like to see why you might want to do this, read on!

Putting it to use: An advanced use case

I recently did some work related to displaying a hierarchical "family tree" in SVG, driven by Raphael and Backbone (that's another post).

Prior to displaying the tree, there was a variable series of checks and prompts that had to be performed, all asynchronous.

It's a mess, and don't focus on it too hard, but the logic I came up with to understand this looks like this:

// Call create endpoint (get guid)
    // If NOT Affiliate Tree
        // Call get endpoint with guid
            // Render
    // Else (Affiliate Tree)
        // When done, check to see if user needs to be warned of billing
            // If Needing to warn, Prompt user
                // If User Accepts
                    // Post to bill
                        // Billing success, create tree
                            // Call get endpoint with guid
                                // Render
                // Else (User Declines (do nothing))
            // Else (No warning)
                // Post to bill
                    // Billing success, create tree
                        // Call get endpoint with guid
                            // Render
 
Now, try and think how you'd do this with callbacks.  Okay, now stop because your brain will explode.

Here's what my overridden fetch method on my Backbone model looked like in the end.  Please note:
  1. This method is pure workflow, no implementation details.
  2. This method returns a promise, so as far as calling fetch goes it's exactly the same as a native fetch implementation (minus the args).
  3. This methods represents variable amounts of async events comprised of AJAX operations and user prompts.
  4. There's no callbacks being passed into my business logic functions, meaning what it means to "promptWarnTree" doesn't care about what happens next, that's the job of the workflow to decide.  The "promptWarnTree" function just lets the workflow know when it's done.
  5. If at any point any promise is rejected, the entire flow ends and the tree is not shown.

fetch: function () {
  var orchestrating = createTree().then(function (type) {
   if (type === 'affiliate') {
    return checkWarnTree().then(function (warn) {
     if (warn) {
      return promptWarnTree().then(function () {
       return billTree();
      });
     }
    });
   }
  });
 
  return orchestrating.then(function () {
   return getTree();
  });
 }

I won't walk you through how this logic all works (for that check out my other posts on promises in this series), but the take-away should be how expressive and compact it is.

Specific implementation examples

In the scenario above, some methods are simple wrappers for other models or normal AJAX calls, but some are user interactions, such as asking the user if they want to accept the billing charges:

function promptWarnTree () {
    // I like to name all of my promise vars with "-ing" suffixes.  Makes things read nicely.
    var prompting = $.Deferred();

    // modalConfirm doesn't implement the Promise API
    // params: message, success, failure
    modalConfirm("Are you sure you wish to incur these charges?", prompting.resolve, prompting.reject);

    return prompting.promise();
}

Passing a value to attached callbacks

Notice how checkWarnTree's done callback has a "warn" parameter which tells the workflow of the result?  Passing a value here is easy via Deferred.resolve() and Deferred.reject() argument passing:

function createTree() {
    var creating = $.post('...').then(function (resp) {
        // In reality this was more complex, but the idea here is that creating is not the result of $.post(), it's a trimmed down version of the response or a derivative value.  In this way, the callbacks attached to creating do not get the full response object, merely the result I want them to have.
        return $.Deferred().resolve(resp.warn);
    });
    return creating;
}

Deferred.resolveWith() and Deferred.rejectWith() are also available and are used to pass a context to the callback as well as args.  I use them infrequently.

Closing

Creating your own promises is a great way to separate concerns in your application, make something that foolishly doesn't implement the promise API do so, and build up deep, long or otherwise variable workflows.