Detailed Use of the JavaScript Library

The following methods are available on the object returned by bomgarState() (the object called allChangesObservable in the example in Basic Use of the JavaScript Library). These methods allow you to subscribe to more focused changes based on table names and change types. Some developers may find these more convenient to use than registering a single callback function for all tables and change types using subscribe() as seen with the basic usage.

.changesForTable(tableName)

Calling this function subscribes only to model changes for the given table name.

Arguments

  • tableName ( String ): The name of a table you requested from bomgarState() using the tables option.

Returns

( Observable<ChangeObject> ): An observable of changes to the given table. Change objects have "name" and "data" properties:

  • {
    • "name": "<change type>", // "insert", "update", or "delete"
    • "data": {
      • "<id>": {
        • // Varies depending on table and change type
        • },
      • // Other rows
    • }
  • }

Example

  • // Get all the change messages for the 'representative' table in one subscription
  • allChangesObservable.changesForTable('representative')
    • .subscribe(function onRepTableChange(repChangeObject) {
      • // 'repChangeObject.name' contains the change name 'insert', 'update', or 'delete'
      • // 'repChangeObject.data' contains the data related to the change from changeObject[change][tableName] int he '.subscribe()' example
      •  
        • switch (repChangeObject.name) {
          • case 'insert':
            • // Use 'repChangeObject.data' to perform an insert
            • break;

            case 'update':

            • // Use 'repChangeObject.data' to perform an update
            • break;

            case 'delete':

            • // Use 'repChangeObject.data' to perform a delete
            • break;
      • }
    • });

.tableInserts(tableName)

Calling this function subscribes only to inserts for the given table name.

Arguments

  • tableName ( String ): The name of a table you requested from bomgarState() using the tables option.

Returns

( Observable<InsertObject> ): An observable of inserts into the given table. Insert objects contain one or more property names that are IDs for the table row and also contain property values that are objects containing the data to insert.

  • {
    • "<id>": {
      • // Varies depending on table
    • },
    • // Other inserts
  • }

Example

  • // Create a subscription for inserts into the 'customer_client' table
  • allChangesObservable.tableInserts('customer_client')
    • .subscribe(function onCustomerClientInsert(insertObject) {
      • // Insert rows
    • });

.tableUpdates(tableName)

Calling this function subscribes only to updates for the given table name.

Arguments

  • tableName ( String ): The name of a table you requested from bomgarState() using the tables option.

Returns

( Observable<UpdateObject> ): An observable of updates for the given table. Update objects contain one or more property names that are IDs for the table row and also contain property values that are objects containing the data to update.

  • {
    • "<id>": {
      • // Varies depending on table
    • },
    • // Other updates
  • }

Example

  • // Create a subscription for updates to the 'customer_client' table
  • allChangesObservable.tableUpdates('customer_client')
    • .subscribe(function onCustomerClientUpdate(updateObject) {
      • // Update rows
    • });

.tableDeletions(tableName)

Calling this function subscribes only to deletions for the given table name.

Arguments

  • tableName ( String ): The name of a table you requested from bomgarState() using the tables option.

Returns

( Observable<Deletion[]> ): An observable of deletions for the given table. Deletion objects are arrays of row IDs:

  • [
    • "<id>",
    • // Other IDs
  • ]

Example

  • // Createa subscription for deletions from the 'customer_client' table
  • allChangesObservable.tableDeletions('customer_client')
    • .subscribe(function onCustomerClientDeletion(deletionObject) {
      • // Delete rows
    • });

 

.truncations()

Calling this function subscribes to notifications that all tables should be truncated.

Arguments

  • None

Returns

( Observable<TruncationObject> ): An observable of truncations. Truncation objects contain a property named type whose value is truncate_model.

Example

  • // Create a subscription for truncations
  • allChangesObservable.truncations()
    • .subscribe(function() {
      • // Delete all data from all tables
    • });