Home > english, Programming > Falling in love with REST APIs and Backbone

Falling in love with REST APIs and Backbone

February 28th, 2013 Leave a comment Go to comments

How I mentioned in my previous post, there is some stuff that I really like about developing with JavaScript. Especially, working with REST APIs and Backbone is so simple. There just a view steps to do. In my case I have chosen the Qype Rest API. Create your model and define your properties you want to work with.

Then create your collection class which handles the requests to the Web Service. For customizing the requests, I added a config method for assigning the correct parameter values. After configuring my collection instance, I called the function CollectionInstance.fetch()… waiting… and voila the data will come in. If you have to parse and manipulate the input data before assigning it to the models, then just overwrite the Collection.parse() function. For an external webservice (beyond your domain) you have to use JSONP. But don’t worry it is already included in Backbone. Just turn on JSONP in the Collection.sync() function.

So far coding the infrastructure and encapsulating the data handling from the app logic. The code in your app is pretty straightforward.

In my example I consumed the QYPE restaurant search API, but please feel free to use any Rest API you want. Happy coding!

  1. No comments yet.
  1. No trackbacks yet.