Batman.js is no longer in production at Shopify and is not actively maintained.

This website is left for reference (and for old times' sake).

batman.js

Batman.SetMapping extends Batman.Set. A Batman.SetMapping tracks a base Set and contains the unique values for a given property for each member of the base Set. It can be created with Batman.Set::mappedTo:

test "mappedTo creates a new Batman.SetMapping", ->
  batmobile = new Batman.Object(name: "Batmobile", wheelCount: 4)
  batcycle = new Batman.Object(name: "Batcycle", wheelCount: 2)
  vehicles = new Batman.Set(batmobile, batcycle)

  vehicleNames = vehicles.mappedTo("name")
  ok vehicleNames.constructor is Batman.SetMapping

It contains the values for the key passed to mappedTo:

  ok vehicleNames.has('Batmobile')
  ok vehicleNames.has('Batcycle')

When an item is added to or removed from the base Batman.Set, its corresponding value is added to or removed from the Batman.SetMapping:

  batwing = new Batman.Object(name: "Batwing", wheelcount: 0)
  vehicles.add(batwing)
  ok vehicleNames.has("Batwing")
  vehicles.remove(batmobile)
  ok !vehicleNames.has("Batmobile")

Like a Batman.SetSort, it tracks the properties of objects in the base set. So, when one of the values changes, the set mapping is updated:

  batwing.set('name', 'Batcopter')
  ok vehicleNames.has('Batcopter')

A Batman.SetMapping can't have duplicates:

  deepEqual vehicleNames.toArray(), ["Batcycle", "Batcopter"]
  batwing.set('name', 'Batcycle')
  deepEqual vehicleNames.toArray(), ["Batcycle"]

Batman.SetMapping extends Batman.Set, so see the Batman.Set API docs for more information.

Using a Batman.SetMapping

Batman.SetMapping is like an observable version of Enumerable.mapToProperty. It performs a get on each member of the base Set and holds unique resulting values.

Since a SetMapping is a proxy of a Set, the easiest way to make one is to use Set::mappedTo:

vehicleNames = vehicles.mappedTo('wheelcount')

You can also make SetMappings in view bindings:

<ul>
  <li data-foreach-vehiclename='vehicles.mappedTo.name'>
    <!-- will render unique vehicle names -->
  </li>
</ul>

SetMappingss are observable proxies of their underlying Sets. So, when the Set is changed (ie, items are added, removed, or modified):

  • The SetMapping is automatically updated by batman.js
  • Any view bindings or accessors depending on the SetMapping are updated

Help us improve our documentation!

Contributions to this page are welcome on Github. If you find a problem but you cannot fix it, please open an issue.

Discussion regarding batman.js documentation is also welcome on our mailing list.