Robert Mosolgo

To my knowledge, batman.js is not maintained. For that reason, I don't suggest that you use it for a new project!

Tips for Batman.RestStorage and Batman.RailsStorage

Just a few things I’ve picked up about Batman.RestStorage and Batman.RailsStorage.

Use Model.url for hooking up to your API

You can set the URL for a model:

1
2
3
4
class App.SomeModel extends Batman.Model
  @persist Batman.RestStorage  # or Batman.RailsStorage
  @resourceName: "some_model"
  @url: "/api/v1/some_models"  # will be used for REST actions (instead of plain `/some_models`)

or for an instance:

1
2
3
parent.url = "/api/v1/some_models/somewhere_special"
parent.url.load ->
  console.log("I loaded from a special place!")

Use autoload carefully

Say you have an association:

1
2
class App.Parent extends Batman.Model
  @hasMany 'children', autoload: trueOrFalse, saveInline: trueOrFalse

If autoload is true, parent.get('children') will send a request to get children items. By default this is /children.json?parent_id=#{parentId}.

You can do a nested url with:

1
2
3
4
class App.Child extends Batman.Model
  @persist Batman.RestStorage
  @belongsTo 'parent'
  @urlNestsUnder 'parent' # will request /parents/:parent_id/children/:id/

saveInline and accepts_nested_nested_attributes_for

saveInline goes nicely with Rails accepts_nested_attributes_for, except that Rails expects a parameter called children_attributes, but batman.js sends children. You can work around it in a couple ways:

1
2
class App.Parent extends Batman.Model
  @hasMany 'children', saveInline: true, encoderKey: 'children_attributes' # this expects children_attributes in JSON from the server, too

And I confess I have done this:

1
2
3
4
5
6
7
8
class App.Parent extends Batman.Model
  @hasMany 'children', saveInline: true

  toJSON: ->
    builtJSON = super
    builtJSON.children_attributes = builtJSON.children
    delete builtJSON.children
    builtJSON

Take advantage of Rails Validation Errors

When Rails responds with 422 and a JSON object with { "errors" : { ... } }, they’ll be added to your model’s errors. data-formfor has a built-in thing for that, so make sure you have an errors div in your form:

1
2
3
<form data-formfor-somemodel='currentSomeModel' data-event-submit='saveSomeModel'>
  <div class='errors'><!-- will be automatically populated --></div>
</form>