Skip to content

Connecting Backbone to XML Services in CoffeeScript

2012 January 24
by Nayan Hajratwala

Jeremy Anderson and I just spent several hours trying to integrate Backbone with a set of XML RESTful services. Backbone comes with built in support for JSON services, but none for XML.

We started by trying to follow this article about using Jath . It seemed promising, but had some holes. Most notable, we were hesitant to make modifications to the core backbone.js. Doing this would have prevented us from easily upgrading to new versions, etc.

The other (slightly) complicating factor was that we were using CoffeeScript and all the example code was in JavaScript.

The current release version of Backbone (0.5.3) does not allow for easily adding the options we needed to the ajax request inside sync(). While perusing the Backbone source, we discovered the the current master version on GitHub had some enhancements that made our lives easier. Downloading the master version, we were now able to pass the new parameters in the options hash.

Be sure not to override the incoming options with a new hash. You must simply append (extend) to the incoming options.

The last step is to get the incoming XML converted to JSON so the rest of Backbone can understand it. Below is an example using Jath as described in the above link.