Friday, June 22, 2012

Sencha Touch And Restful Models (My Solution)

For those that have been sitting on the edges of their seats waiting for this post, here it is. Finally!

To start off, let's here is a list of my goals.
  • Establish an automated method for retrieving a child model from a RESTful service.
  • Reduce the application code to manage multiple result sets of slight variation.
  • Reduce the code complexity for working with RESTful services and Sencha Touch.
  • Simplify the development experience with Sencha Touch and model associations.
The pattern that I immediately see with any RESTful service is an initial subset of data that contains a URI.  To understand the full data set for the same RESTful service, a client would navigate the URI to retrieve the rest of the service details.  We end up with something like this.
{ id: 0, uri: 'makers/0' }
Once the uri of the makers Model is expanded, you should see something like this which contains a model of Chevrolet that I have owned.
{ id: 0, name: 'Chevrolet', models: [ { id: 0, maker_id: 0, name: 'S10', year: '2003', uri: 'cars/0/0' }, ... ] }
Inside the models array, there are potentially multiple records that will next need to be expanded. The complicating factor is the need to navigate and further expand the data sets that you see once the user requires more information. In the above example data set, I have chosen these fields to show on the initial view. I have identified these fields to display to the user to present them with enough information to make their next choice.

View my screencast to see this in action.



You can download the code to my solution from my github project, RestModel.  The implementation with some examples and unit tests are all included.  Please leave a comment if you have any questions or need assistance with setting this up and running with it.

2 comments:

  1. Thanks Matt, Awesome work.
    Do you believe I can replace the URI with an actual RESTful webervice call and request the return to be json?

    ReplyDelete
  2. Hi Josh,

    (Sorry about the late response. I have to figure out the email notifications here.)

    The references I show are relative links. I am pretty certain you can specify a fully qualified url. Have you experimented with this at all? Have you add any success with the model?

    Matt

    ReplyDelete