Event API for Custom Resources #

Background #

Custom Resources may load event scripts to allow you to inject business logic during requests to the resource. For example, the collection resource exposes an event called validate. Given the following todo resource folder:

/my-app
  /resources
    /todos
      validate.js

The collection resource would load the contents of validate.js as the validate event.

Default Event Script Domain #

Event scripts do not share a global scope with other modules in your app. Instead each time an event script is run, a new scope is created for it.

The following functions and objects are available to all event scripts.

ctx #

The context of the request. This object contains everything from the request (request, response, body, headers, etc...):

// Example:
if (ctx && ctx.req && ctx.req.headers && ctx.req.headers.host !== '192.168.178.34:2403') {
  cancel("You are not authorized to do that", 401);
}

The entire object is available as a gist here.

me #

The current user if one exists on the current Context.

isMe() #

isMe(id)

Returns true if the current user (me) matches the provided id.

this #

If the resource does not implement a custom domain, this will be an empty object. Otherwise this usually refers to the current domain's instance (eg. an object in a collection).

cancel() #

cancel(message, [statusCode])

Stops the current request with the provided error message and HTTP status code. Status code defaults to 400.

cancelIf(), cancelUnless() #

cancelIf(condition, message, [statusCode])
cancelUnless(condition, message, [statusCode])

Calls cancel(message, statusCode) if the provided condition is truthy (for cancelIf()) or falsy (for cancelUnless).

internal #

A boolean property, true if this request has been initiated by another script.

isRoot #

A boolean property, true if this request is authenticated as root (from the dashboard or a custom script).

query #

The current HTTP query. (eg ?foo=bar - query would be {foo: 'bar'}).

emit() #

emit([userCollection, query], message, [data])

Emits a realtime message to the client. You can use userCollection and query parameters to limit the message broadcast to specific users.

console #

Support for console.log() and other console methods.

More

Other docs in "Reference"