- Initialize the base URL.
- Define the syncable object.
- Define the schema and initialize the entity store with that schema.
- Get an instance of the
AK.PersistenceManager and call its
- Close any open connection to the AK.PersistenceManager.
The following sections show examples of these tasks.
Initializing the app scope base URL
You must call the
init() method takes a single argument, the URL to the base of your app scope.
The following example calls the
Defining a syncable object
firstname– A string representing a customer's first name.
lastname– A string representing a customer's last name.
custid– An integer representing the customer's ID. This field is also used as the index and primary key.
Defining a schema and initializing the entity store
The following code shows how the client might define a schema and initialize the entity store with that schema:
The names of the fields must match the properties of the object you defined in the previous step.
In this example, the schema does not define an index field. The key field is used as the index by default.
The field names in the model and in the corresponding schema definition do not need to match the corresponding names of the fields on the back end data store. However, you must adhere to any contract that you might have with the app scope so that your field names can be matched to those that the Akula project developer expects.
Calling the sync() method
After you have defined a schema and initialiazed the entity store, you can call the
sync() method. The
sync() method gets the data from the back-end server and populates the client's persistence data store with that data.
sync() method has the following signature:
sync( entityClass, syncType, [syncEndpoint] )
entityClass argument specifies that name of the class that defines the object type you want to persist. You pass this as a string. For example, "Customer".
syncType argument defines the type of sync operation that you want to perform. It takes the following constants: "full", "incremental", and "none". A full sync gets all records from the back-end data source. An incremental sync can get a subset of records from the back-end data store, but what it actually returns is based on how you write your custom synchronization module.
syncEndpoint argument defines the location of the route that carries out sync operations. This value is relative to the base URL that you defined when you called the
AK.init() method. This argument is optional. You can also specify the sync endpoint in the
sync() method returns a promise. When the method eventually returns the results, the argument passed to the
onFulfilled handler is an
AK.SyncResult object. This object contains the sync type and the records from the back-end that will be persisted on the client.
The argument passed to the
onRejected handler contains the error message for why the operation failed.
The following example gets an instance of the AK.PersistenceManager and calls the
If the sync operation is only partially successful, then there are typically revert records. Revert records are records that the client updated but that the server rejected for some reason. It could be that some other user updated the record, the record was locked, or the record was deleted. For more information on handling revert records, see Handling Synchronization Success and Failure .
Closing the persistence manager