# Object

## AV. Object

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

## Constructor

#### new Object(attributes, options)

Source:
  - object.js, line 59
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) query :AV.Query

Source:
  - object.js, line 1452
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 1343
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 1263

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 1276

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 1369

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 128

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 1438

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 116

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 765

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 777

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.

#### change()

Source:
  - object.js, line 1063

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 1126

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 809

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

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

Source:
  - object.js, line 1046

Creates a new model with identical attributes to this one.

##### Returns:

Type
  AV.Object

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

Source:
  - object.js, line 999

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 283

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 350

Gets the HTML-escaped value of an attribute.

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

Source:
  - object.js, line 847

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 174
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 317

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 1211
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 194

Returns the object's createdAt attribute.

##### Returns:

Type
  Date

#### getObjectId() → {String}

Source:
  - object.js, line 186

Returns the object's objectId.

##### Returns:

the objectId.

Type
  String

#### getUpdatedAt() → {Date}

Source:
  - object.js, line 202

Returns the object's updatedAt attribute.

##### Returns:

Type
  Date

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

Source:
  - object.js, line 372

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 1111

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 752

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 164

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

#### isNew() → {Boolean}

Source:
  - object.js, line 1054

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

##### Returns:

Type
  Boolean

#### isValid() → {Boolean}

Source:
  - object.js, line 1166

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 801

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 1145

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 1157

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 334

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 788

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.

#### save(options) → {AV.Promise}

Source:
  - object.js, line 887
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
  AV.Promise

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

Source:
  - object.js, line 632
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
    }, {
      error: function(gameTurnAgain, error) {
        // The set failed validation.
      }
    });

    game.set("currentPlayer", player2, {
      error: function(gameTurnAgain, error) {
        // The set failed validation.
      }
    });

    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) → {Boolean}

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

Whether the set passed validation.

Type
  Boolean

#### toFullJSON() → {Object}

Source:
  - object.js, line 220
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 210

Returns a JSON version of the object.

##### Returns:

Type
  Object

#### unset()

Source:
  - object.js, line 739

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 1185
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.