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