Skip to content

Registering ActiveRecords

Nikos Zinas edited this page May 17, 2014 · 1 revision

To register a new ActiveRecord, you need to use the register() function. A simple example is the following.

var Car = ActiveRecord.register("Car");

This will wire up your AR with a specific endpoint, which is constructed with the following formula: Base URL + Endpoint Root + "Car".toLowerCase.toPlural

By default, it will look something like this: /activerecord.js/rest/cars

Configuration

There are 2 points of extension for each AR: configuration and functionality. Configuration is about properties used for the AR to work correctly, while functionality is about extending the AR itself with extra features.

Configuration

You can change configuration options for each AR, but passing another parameter in the register function. Here is an example

var Car = ActiveRecord.register("Car", {
   url : '/carsCompany/rest/userCars' // instead of /activerecord.js/rest/cars
});

Here are the available options

  • url The endpoint for the ActiveRecord
  • primaryKey The property that is considered to be the primary key for the AR
  • columns An array with the database columns. If none is provided, an ajax call will happen to read the columns from the API

Functionality

You can extend functionality, or even override default functions with the extend() method

Car.extend({
  beforeSave : function () {
    // hook to execute before saving the AR
  },
  save : function () {
    // totally override the default function save() (not recommended!)
    // you have access to the original function via this._super.save()
  },
  paintCar : function () {
    // You can add your own functions and invoke them whenever you want
  }
});

All the available options will be explained in the next pages of the API Documentation