Using Apex REST Resources

To support Apex REST resources, Mobile SDK provides two classes: Force.ApexRestObject and Force.ApexRestObjectCollection. These classes subclass Force.RemoteObject and Force.RemoteObjectCollection, respectively, and can talk to a REST API that you have created using Apex REST.

Force.ApexRestObject is similar to Force.SObject. Instead of an sobjectType, Force.ApexRestObject requires the Apex REST resource path relative to services/apexrest. For example, if your full resource path is services/apexrest/simpleAccount/*, you specify only /simpleAccount/*. Force.ApexRestObject also expects you to specify the name of your ID field if it's different from "Id".

Let's assume you’ve created an Apex REST resource called "simple account," which is just an account with two fields: accountId and accountName.

With Mobile Sync, you do the following to create a "simple account".

You can update that "simple account".

You can fetch another "simple account".

You can delete a "simple account".

In Mobile Sync calls such as fetch(), save(), and destroy(), you typically pass an options parameter that defines success and error callback functions. For example:

acc.destroy({success:function(){alert("delete succeeded");}});

Force.ApexRestObjectCollection is similar to Force.SObjectCollection. The config you specify for fetching doesn't support SOQL, SOSL, or MRU. Instead, it expects the Apex REST resource path, relative to services/apexrest. For example, if your full resource path is services/apexrest/simpleAccount/*, you specify only /simpleAccount/*.

You can also pass parameters for the query string if your endpoint supports them. The Apex REST endpoint is expected to return a response in this format:

Let's assume you’ve created an Apex REST resource called "simple accounts". It returns "simple accounts" that match a given name.

With Mobile Sync, you do the following to fetch a list of "simple account" records.