Saving Data Objects

Top  Previous  Next

The API to save an object can be used for two separate scenarios: if an object has been previously saved, it is updated in the data store, otherwise it is saved (created). The save operation checks if the object has objectId assigned by the server. in that case, the object is updated, otherwise it is created in the Backendless data store. In case when there is no table for the persisted object, Backendless creates one and maps table's columns to the object's properties.

The objectId property is automatically assigned to all persisted objects when they are initially saved. See the Data Object section for details on objectId.

 

Backendless.Persistence.of( constructor-function ).save( entity, async )

where:

constructor-function- a reference to a JavaScript constructor function defining a JavaScript class. The entity argument must be an instance of this class.
entity- a JavaScript object to persist, must be an instance of JS-Class.
async- Optional argument. If provided, the method is executed asynchronously. The successCallback function referenced in the async object receives a callback when the method successfully saves the object. If an error occurs, the faultCallback function is invoked. See Sync and Async Callsfor additional details.

Return Value:

If the method is invoked synchronously, it returns the saved object. The asynchronous call receives the return value through a callback executed on the Async object.

Example:

Consider the following class:

function Contact(args) {
    args = args || {};
    this.name = args.name || "";
    this.age = args.age || "";
    this.phone = args.phone || "";
    this.title = args.title || "";
}

The following code saves a new instance of the Contact class:

var APPLICATION_ID = "YOUR-APP-ID",
    SECRET_KEY = "YOUR-SECRET-KEY",
    VERSION = "v1";   //default application version;

Backendless.initApp( APPLICATION_ID, SECRET_KEY, VERSION );

var contactObject = new Contact( {
    name: "James Bond",
    age: 45,
    phone: "1-800-JAMESBOND",
    title: "chief spying officer"
});

var savedContact = Backendless.Persistence.of( Contact ).save( contactObject );

 

 


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: