# Object

## AV. Object

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

## Constructor

#### new Object(attributes, options)

Source:
  - object.js, line 78
See:
  - AV.Object.extend

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:
  - object.js, line 1601
Since:
  - v3.1.0

Get a new Query of the current class

##### Type:

- AV.Query

##### Example

    const Post = AV.Object.extend('Post');
    Post.query.equalTo('author', 'leancloud').find().then();

### Methods

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

Source:
  - object.js, line 1482
Since:
  - v0.4.4
See:
  - AV.Object
  - AV.Object.extend

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(className, id) → {AV.Object}

Source:
  - object.js, line 1393

Creates an instance of a subclass of AV.Object for the give classname and id.

##### Parameters:

Name | Type | Description
--- | --- | ---
`className` | String | The name of the AV class backing this model.
`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:
  - object.js, line 1406

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:
  - object.js, line 1508

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:
  - object.js, line 147

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:
  - object.js, line 1587

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:
  - object.js, line 135

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(attr, item)

Source:
  - object.js, line 808

Atomically add an object to the end of the array associated with a given key.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The key.
`item` |  | The item to add.

#### addUnique(attr, item)

Source:
  - object.js, line 820

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
--- | --- | ---
`attr` | String | The key.
`item` |  | The object to add.

#### bitAnd(attr, value)

Source:
  - object.js, line 842

Atomically apply a "bit and" operation on the value associated with a given key.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The key.
`value` | Number | The value to apply.

#### bitOr(attr, value)

Source:
  - object.js, line 853

Atomically apply a "bit or" operation on the value associated with a given key.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The key.
`value` | Number | The value to apply.

#### bitXor(attr, value)

Source:
  - object.js, line 864

Atomically apply a "bit xor" operation on the value associated with a given key.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The key.
`value` | Number | The value to apply.

#### change()

Source:
  - object.js, line 1181

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.

#### changedAttributes()

Source:
  - object.js, line 1244

Returns an object containing all the attributes that have changed, or false if there are no changed attributes. Useful for determining what parts of a view need to be updated and/or what attributes need to be persisted to the server. Unset attributes will be set to undefined. You can also pass an attributes object to diff against the model, determining if there *would be* a change.

#### clear()

Source:
  - object.js, line 885

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

#### clone() → {AV.Object}

Source:
  - object.js, line 1164

Creates a new model with identical attributes to this one.

##### Returns:

Type
  AV.Object

#### destroy(options) → {Promise}

Source:
  - object.js, line 1111

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:
  - object.js, line 320

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

#### escape()

Source:
  - object.js, line 389

Gets the HTML-escaped value of an attribute.

#### fetch(fetchOptions, options) → {Promise}

Source:
  - object.js, line 937

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:
  - object.js, line 208
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:
  - object.js, line 356

Gets the value of an attribute.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The string name of an attribute.

#### getACL() → {AV.ACL}

Source:
  - object.js, line 1328
See:
  - AV.Object#get

Returns the ACL for this object.

##### Returns:

An instance of AV.ACL.

Type
  AV.ACL

#### getCreatedAt() → {Date}

Source:
  - object.js, line 230

Returns the object's createdAt attribute.

##### Returns:

Type
  Date

#### getObjectId() → {String}

Source:
  - object.js, line 222

Returns the object's objectId.

##### Returns:

the objectId.

Type
  String

#### getUpdatedAt() → {Date}

Source:
  - object.js, line 238

Returns the object's updatedAt attribute.

##### Returns:

Type
  Date

#### has(attr) → {Boolean}

Source:
  - object.js, line 411

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

#### hasChanged(attr) → {Boolean}

Source:
  - object.js, line 1229

Determine if the model has changed since the last `"change"` event. If you specify an attribute name, determine if that attribute has changed.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | Optional attribute name

##### Returns:

Type
  Boolean

#### increment(attr, amount)

Source:
  - object.js, line 795

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
--- | --- | ---
`attr` | String | The key.
`amount` | Number | The amount to increment by.

#### initialize()

Source:
  - object.js, line 198

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

#### isNew() → {Boolean}

Source:
  - object.js, line 1172

Returns true if this object has never been saved to AV.

##### Returns:

Type
  Boolean

#### isValid() → {Boolean}

Source:
  - object.js, line 1284

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(attr) → {AV.Op}

Source:
  - object.js, line 877

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
--- | --- | ---
`attr` | String | The key.

##### Returns:

The operation, or undefined if none.

Type
  AV.Op

#### previous(attr)

Source:
  - object.js, line 1263

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:
  - object.js, line 1275

Gets all of the attributes of the model at the time of the previous `"change"` event.

##### Returns:

Type
  Object

#### relation(attr) → {AV.Relation}

Source:
  - object.js, line 373

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(attr, item)

Source:
  - object.js, line 831

Atomically remove all instances of an object from the array associated with a given key.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | The key.
`item` |  | The object to remove.

#### revert(keysopt)

Source:
  - object.js, line 896

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:
  - object.js, line 990
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:
  - object.js, line 671
See:
  - AV.Object#validate

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:
  - object.js, line 1340
See:
  - AV.Object#set

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:
  - object.js, line 256
Since:
  - 2.0.0

Returns a JSON version of the object with meta data. Inverse to AV.parseJSON

##### Returns:

Type
  Object

#### toJSON() → {Object}

Source:
  - object.js, line 246

Returns a JSON version of the object.

##### Returns:

Type
  Object

#### unset()

Source:
  - object.js, line 782

Remove an attribute from the model, firing `"change"` unless you choose to silence it. This is a noop if the attribute doesn't exist.

#### validate(attrs)

Source:
  - object.js, line 1303
See:
  - AV.Object#set

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.