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!

Load Batman.js Models From Specific Urls

Batman.js’s REST Storage Adapter provides a clean interface for operating on records with vanilla REST urls – but what about when you need a lil’ something more?

First of all, the magic happens in Batman.RestStorage#UrlForRecord. For an already-persisted record, you have a few options:

pass recordUrl to loadWithOptions/findWithOptions

findWithOptions or loadWithOptions’s options can take a recordUrl param, which is used to retrieve the record. Use findWithOptions to load a new record:

1
2
3
4
  model_id = 1 # you might get this from the request path or from another model's attributes
  model_url = "/some_models/#{model_id}?language=fr"
  MyApp.SomeModel.findWithOptions model_id, {recordUrl: model_url}, (err, model) ->
    # your model was loaded with the param language=fr!

or use loadWithOptions to reload an existing record:

1
2
3
4
  # using model from above...
  model_url = "/some_models/#{model.get('id')}?language=es"
  model.loadWithOptions {recordUrl: model_url}, (err, model) ->
    # your model was loaded with the param language=es!

Set record.url

If you’re reloading an already-loaded model, you can set its (POJO) url attribute to the URL you want to use:

1
2
3
  model.url = "/#{model.constructor.storageKey}/#{model.get('id')}?language=zh"
  model.load (err, model) ->
    # your model was loaded with the param language=zh!

For a New Records or Collections?

I’m not sure yet. Check out Batman.RestStorage#UrlForCollection!