Object

AV. Object

The fundamental unit of AV data, which implements the Backbone Model interface.

Constructor

new Object(attributes, options)

Source:
See:
Creates a new model with defined attributes. A client id (cid) is automatically generated and assigned for you.

You won't normally call this method directly. It is recommended that you use a subclass of AV.Object instead, created by calling extend.

However, if you don't want to use a subclass, or aren't sure which subclass is appropriate, you can use this form:

    var object = new AV.Object("ClassName");
That is basically equivalent to:
    var MyClass = AV.Object.extend("ClassName");
    var object = new MyClass();

Parameters:
Name Type Description
attributes Object The initial set of data to store in the object.
options Object A set of Backbone-like options for creating the object. The only option currently supported is "collection".

Members

(static, readonly) query :AV.Query

Source:
Since:
  • v3.1.0
Get a new Query of the current class
Type:
Example
const Post = AV.Object.extend('Post');
Post.query.equalTo('author', 'leancloud').find().then();

Methods

(static) 'new'(attributes, options) → {AV.Object}

Source:
Since:
  • v0.4.4
See:
Creates a new model with defined attributes, It's the same with
  new AV.Object(attributes, options);
 
Parameters:
Name Type Description
attributes Object The initial set of data to store in the object.
options Object A set of Backbone-like options for creating the object. The only option currently supported is "collection".
Returns:
Type
AV.Object

(static) createWithoutData(class, id) → {AV.Object}

Source:
Creates an instance of a subclass of AV.Object for the give classname and id.
Parameters:
Name Type Description
class String | function the className or a subclass of AV.Object.
id String The object id of this model.
Returns:
A new subclass instance of AV.Object.
Type
AV.Object

(static) destroyAll(objects, options) → {Promise}

Source:
Delete objects in batch.
Parameters:
Name Type Description
objects Array.<AV.Object> The AV.Object array to be deleted.
options AuthOptions
Returns:
A promise that is fulfilled when the save completes.
Type
Promise

(static) extend(className, protoProps, classProps) → {Class}

Source:
Creates a new subclass of AV.Object for the given AV class name.

Every extension of a AV class will inherit from the most recent previous extension of that class. When a AV.Object is automatically created by parsing JSON, it will use the most recent extension of that class.

Example
var MyClass = AV.Object.extend("MyClass", {
    // Instance properties
}, {
    // Class properties
});
Parameters:
Name Type Description
className String The name of the AV class backing this model.
protoProps Object Instance properties to add to instances of the class returned from this method.
classProps Object Class properties to add the class returned from this method.
Returns:
A new subclass of AV.Object.
Type
Class

(static) fetchAll(objects, options) → {Promise.<Array.<AV.Object>>}

Source:
Fetch the given list of AV.Object.
Parameters:
Name Type Description
objects Array.<AV.Object> A list of AV.Object
options AuthOptions
Returns:
The given list of AV.Object, updated
Type
Promise.<Array.<AV.Object>>

(static) register(klass, nameopt)

Source:
Register a class. If a subclass of AV.Object is defined with your own implement rather then AV.Object.extend, the subclass must be registered.
Example
class Person extend AV.Object {}
AV.Object.register(Person);
Parameters:
Name Type Attributes Description
klass function A subclass of AV.Object
name String <optional>
Specify the name of the class. Useful when the class might be uglified.

(static) saveAll(list)

Source:
Saves the given list of AV.Object. If any error is encountered, stops and calls the error handler.
Example
AV.Object.saveAll([object1, object2, ...]).then(function(list) {
  // All the objects were saved.
}, function(error) {
  // An error occurred while saving one of the objects.
});
Parameters:
Name Type Description
list Array A list of AV.Object.

add(key, item)

Source:
Atomically add an object to the end of the array associated with a given key.
Parameters:
Name Type Description
key String The key.
item The item to add.

addUnique(key, item)

Source:
Atomically add an object to the array associated with a given key, only if it is not already present in the array. The position of the insert is not guaranteed.
Parameters:
Name Type Description
key String The key.
item The object to add.

bitAnd(key, value)

Source:
Atomically apply a "bit and" operation on the value associated with a given key.
Parameters:
Name Type Description
key String The key.
value Number The value to apply.

bitOr(key, value)

Source:
Atomically apply a "bit or" operation on the value associated with a given key.
Parameters:
Name Type Description
key String The key.
value Number The value to apply.

bitXor(key, value)

Source:
Atomically apply a "bit xor" operation on the value associated with a given key.
Parameters:
Name Type Description
key String The key.
value Number The value to apply.

change()

Source:
Call this method to manually fire a `"change"` event for this model and a `"change:attribute"` event for each changed attribute. Calling this will cause all objects observing the model to update.

clear()

Source:
Clear all attributes on the model, firing "change" unless you choose to silence it.

clone() → {AV.Object}

Source:
Creates a new model with identical attributes to this one.
Returns:
Type
AV.Object

destroy(options) → {Promise}

Source:
Destroy this model on the server if it was already persisted. Optimistically removes the model from its collection, if it has one.
Parameters:
Name Type Description
options AuthOptions AuthOptions plus:
Properties
Name Type Attributes Description
wait Boolean <optional>
wait for the server to respond before removal.
Returns:
A promise that is fulfilled when the destroy completes.
Type
Promise

dirty(attr) → {Boolean}

Source:
Returns true if this object has been modified since its last save/refresh. If an attribute is specified, it returns true only if that particular attribute has been modified since the last save/refresh.
Parameters:
Name Type Description
attr String An attribute name (optional).
Returns:
Type
Boolean

dirtyKeys() → {Array.<String>}

Source:
Returns the keys of the modified attribute since its last save/refresh.
Returns:
Type
Array.<String>

escape()

Source:
Gets the HTML-escaped value of an attribute.

fetch(fetchOptions, options) → {Promise}

Source:
Fetch the model from the server. If the server's representation of the model differs from its current attributes, they will be overriden, triggering a "change" event.
Parameters:
Name Type Description
fetchOptions Object Optional options to set 'keys', 'include' and 'includeACL' option.
options AuthOptions
Returns:
A promise that is fulfilled when the fetch completes.
Type
Promise

fetchWhenSave(enable)

Source:
Deprecated:
  • use AV.Object#save with options.fetchWhenSave instead
Set whether to enable fetchWhenSave option when updating object. When set true, SDK would fetch the latest object after saving. Default is false.
Parameters:
Name Type Description
enable boolean true to enable fetchWhenSave option.

get(attr)

Source:
Gets the value of an attribute.
Parameters:
Name Type Description
attr String The string name of an attribute.

getACL() → {AV.ACL}

Source:
See:
Returns the ACL for this object.
Returns:
An instance of AV.ACL.
Type
AV.ACL

getCreatedAt() → {Date}

Source:
Returns the object's createdAt attribute.
Returns:
Type
Date

getObjectId() → {String}

Source:
Returns the object's objectId.
Returns:
the objectId.
Type
String

getUpdatedAt() → {Date}

Source:
Returns the object's updatedAt attribute.
Returns:
Type
Date

has(attr) → {Boolean}

Source:
Returns true if the attribute contains a value that is not null or undefined.
Parameters:
Name Type Description
attr String The string name of the attribute.
Returns:
Type
Boolean

increment(key, amount)

Source:
Atomically increments the value of the given attribute the next time the object is saved. If no amount is specified, 1 is used by default.
Parameters:
Name Type Description
key String The key.
amount Number The amount to increment by.

initialize()

Source:
Initialize is an empty function by default. Override it with your own initialization logic.

isNew() → {Boolean}

Source:
Returns true if this object has never been saved to AV.
Returns:
Type
Boolean

isValid() → {Boolean}

Source:
Checks if the model is currently in a valid state. It's only possible to get into an *invalid* state if you're using silent changes.
Returns:
Type
Boolean

op(key) → {AV.Op}

Source:
Returns an instance of a subclass of AV.Op describing what kind of modification has been performed on this field since the last time it was saved. For example, after calling object.increment("x"), calling object.op("x") would return an instance of AV.Op.Increment.
Parameters:
Name Type Description
key String The key.
Returns:
The operation, or undefined if none.
Type
AV.Op

previous(attr)

Source:
Gets the previous value of an attribute, recorded at the time the last "change" event was fired.
Parameters:
Name Type Description
attr String Name of the attribute to get.

previousAttributes() → {Object}

Source:
Gets all of the attributes of the model at the time of the previous "change" event.
Returns:
Type
Object

relation(attr) → {AV.Relation}

Source:
Gets a relation on the given class for the attribute.
Parameters:
Name Type Description
attr String The attribute to get the relation for.
Returns:
Type
AV.Relation

remove(key, item)

Source:
Atomically remove all instances of an object from the array associated with a given key.
Parameters:
Name Type Description
key String The key.
item The object to remove.

revert(keysopt)

Source:
Clears any (or specific) changes to the model made since the last save.
Parameters:
Name Type Attributes Description
keys string | Array.<string> <optional>
specify keys to revert.

save(options) → {Promise}

Source:
See:
  • AVError
Set a hash of model attributes, and save the model to the server. updatedAt will be updated when the request returns. You can either call it as:
  object.save();
or
  object.save(null, options);
or
  object.save(attrs, options);
or
  object.save(key, value, options);
Example
gameTurn.save({
  player: "Jake Cutter",
  diceRoll: 2
}).then(function(gameTurnAgain) {
  // The save was successful.
}, function(error) {
  // The save failed.  Error is an instance of AVError.
});
Parameters:
Name Type Description
options AuthOptions AuthOptions plus:
Properties
Name Type Description
fetchWhenSave Boolean fetch and update object after save succeeded
query AV.Query Save object only when it matches the query
Returns:
A promise that is fulfilled when the save completes.
Type
Promise

set(key, value, optionsopt) → {AV.Object}

Source:
See:
Sets a hash of model attributes on the object, firing "change" unless you choose to silence it.

You can call it with an object containing keys and values, or with one key and value. For example:

Example
gameTurn.set({
  player: player1,
  diceRoll: 2
});

game.set("currentPlayer", player2);

game.set("finished", true);
Parameters:
Name Type Attributes Description
key String The key to set.
value Any The value to give it.
options Object <optional>
Properties
Name Type Attributes Description
silent Boolean <optional>
Returns:
self if succeeded, throws if the value is not valid.
Type
AV.Object

setACL(acl, options) → {AV.Object}

Source:
See:
Sets the ACL to be used for this object.
Parameters:
Name Type Description
acl AV.ACL An instance of AV.ACL.
options Object Optional Backbone-like options object to be passed in to set.
Returns:
self
Type
AV.Object

toFullJSON() → {Object}

Source:
Since:
  • 3.0.0
Returns a JSON version of the object with meta data. Inverse to AV.parseJSON
Returns:
Type
Object

toJSON() → {Object}

Source:
Returns a JSON version of the object.
Returns:
Type
Object

unset(key)

Source:
Remove an attribute from the model, firing "change" unless you choose to silence it. This is a noop if the attribute doesn't exist.
Parameters:
Name Type Description
key String The key.

validate(attrs)

Source:
See:
You should not call this function directly unless you subclass AV.Object, in which case you can override this method to provide additional validation on set and save. Your implementation should throw an Error if the attrs is invalid
Parameters:
Name Type Description
attrs Object The current data to validate.