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!

Loading Child Records from Embedded IDs in Batman.js

Batman.js provides a powerful model associations inspired by Ruby on Rails. But, if you’re loading child items from ids, it’s not going to work out of the box.

The problem

Batman.js @hasMany (in v0.15.0, anyways) doesn’t support loading items from JSON like this:

  {
    "parent" : {
      "id": 1,
      "children": [10, 11, 12] /* <- here's tough part */
    }
  }

The solution

Instead of Model.hasMany, use a custom encoder to load the records:

class Parent extends Batman.Model
  @encode 'children',
    encode: (value, key, builtJSON, record) ->
      ids = value.mapToProperty('id')
      builtJSON.key = ids

    decode: (value, key, incomingJSON, outgoingObject, record) ->
      ids = value
      childRecords = new Batman.Set
      ids.forEach (id) ->
        child = new Child # <-- your Child class here
        child.set('id', id)
        child.load()
        childRecords.add(child) # one caveat -- the childRecords' attributes will be empty until their requests come back.
      childRecords.set('loaded', true)
      outgoingObject.key = childRecords
      record.set(key, childRecords) # this will fire updates in case bindings are waiting for this data

Make sure to add your own Child class! Also, note that their attributes will be empty until their AJAX requests resolve!