Backbone Integration

matthew342 edited this page Jul 25, 2012 · 7 revisions

Backbone Integration

A good practice when using client-side frameworks such as Backbone.js or Ember.js is to pre-fetch JS models when serving the views from the server for the first time. This saves the initial HTTP request usually required to bootstrap the JS.

In order to pre-fetch JS models using RABL, one just need to render the relevant RABL template inline, within the view. For instance:

Create a partial for the object, this has to be named like app/views/foo/_bar.json.rabl. Note the leading underscore to indicate a partial. It is not mandatory. If you need to reuse the rabl template as a legacy view, discard the leading underscore. In your HTML view:

render(file: 'foo/bar', object: foo_object, formats: :json).html_safe

where it is assumed the rabl template makes use of a @object variable. This is not as clean as it could be, but this is 99% of the way there and it works.