Home Reference Source Repository
import ParseModel from 'backbone-parse-es6/src/ParseModel.js'
public class | source

ParseModel

You can directly use instance of this class. parseModel

Extends:

backbone-es6/src/Model.js~Model → ParseModel

ParseModel - Models are the heart of any JavaScript application. (http://backbonejs.org/#Model)

This implementation of Backbone.Model is backed by a ParseObject. If a ParseObject is not provided in options then a className for the associated table must be defined as options.className or a getter method such as get className() { return '<CLASSNAME>'; }. All methods that trigger synchronization return an ES6 Promise or a ParsePromise. This includes the following methods: destroy, fetch, save. Rather than passing in a error or success callback one can use promises to post a follow up chain of actions to complete.

Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control.

Backbone-Parse-ES6 supports the older "extend" functionality of the Parse SDK. You can still use "extend" to extend Backbone.Model with your domain-specific methods, and Model provides a basic set of functionality for managing changes. Refer to modelExtend which provides the "extend" functionality for ParseModel. It differs from the standard Backbone extend functionality such that the first parameter requires a class name string for the associated table.

It is recommended though to use ES6 syntax for working with Backbone-Parse-ES6 foregoing the older "extend" mechanism.

Create a new model with the specified attributes. A client id (cid) is automatically generated & assigned for you.

If you pass a {collection: ...} as the options, the model gains a collection property that will be used to indicate which collection the model belongs to, and is used to help compute the model's url. The model.collection property is normally created automatically when you first add a model to a collection. Note that the reverse is not true, as passing this option to the constructor will not automatically add the model to the collection. Useful, sometimes.

If {parse: true} is passed as an option, the attributes will first be converted by parse before being set on the model.

Please see the Model documentation for relevant information about the parent class / implementation.

Example:

import Backbone from 'backbone';

export default class MyModel extends Backbone.Model
{
   initialize() { alert('initialized!); }
}

older extend example:
export default Backbone.Model.extend('<CLASSNAME>',
{
   initialize: { alert('initialized!); }
});
The following methods return a promise - destroy, fetch, save. An example on using promises for save:

model.save().then(() =>
{
   // success
},
(error) =>
{
   // error
});

Constructor Summary

Public Constructor
public

constructor(attributes: object, options: object)

When creating an instance of a model, you can pass in the initial values of the attributes, which will be set on the model.

Member Summary

Public Members
public

The hash of attributes for this model.

public

A hash of attributes whose current and previous value differ.

public

Client side ID

public

The prefix is used to create the client id which is used to identify models locally.

public

Parse class name

public

collection: Collection

A potentially associated collection.

public

id: *

Update the id.

public

parseObject: string | ParseObject

Parse class name string or proxy ParseObject

public

The value returned during the last failed validation.

Method Summary

Public Methods
public

clone(): *

Returns a new instance of the model with identical attributes.

public

destroy(options: object): Promise | ParsePromise

Destroys the model on the server by delegating delete request to Backbone.sync and the associated ParseObject.

public

Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.

public

parse(resp: object, options: object): object

parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model. This implementation requires a ParseObject and the attributes are directly taken from the attributes of the ParseObject. To keep parity with the Parse SDK the ID of the ParseObject is set as this.id.

public

set(key: object | string, val: * | object, options: object): *

Set a hash of attributes (one or many) on the model and potentially on the associated ParseObject.

public

Return a copy of the model's attributes object.

public

url()

This is an unsupported operation for backbone-parse-es6.

Public Constructors

public constructor(attributes: object, options: object) source

When creating an instance of a model, you can pass in the initial values of the attributes, which will be set on the model. If you define an initialize function, it will be invoked when the model is created.

Params:

NameTypeAttributeDescription
attributes object

Optional attribute hash of original values to set.

options object

Optional parameters

Public Members

public attributes: object source

The hash of attributes for this model.

public changed: object source

A hash of attributes whose current and previous value differ.

public cid: number source

Client side ID

public cidPrefix: string source

The prefix is used to create the client id which is used to identify models locally. You may want to override this if you're experiencing name clashes with model ids.

public className: string source

Parse class name

public collection: Collection source

A potentially associated collection.

public id: * source

Update the id.

public parseObject: string | ParseObject source

Parse class name string or proxy ParseObject

public validationError: * source

The value returned during the last failed validation.

Public Methods

public clone(): * source

Returns a new instance of the model with identical attributes.

Return:

*

See:

public destroy(options: object): Promise | ParsePromise source

Destroys the model on the server by delegating delete request to Backbone.sync and the associated ParseObject. Returns ParsePromise or ES6 Promise if the model isNew. Accepts success and error callbacks in the options hash, which will be passed (model, response, options). Triggers a "destroy" event on the model, which will bubble up through any collections that contain it, and a "sync" event, after the server has successfully acknowledged the model's deletion. Pass {wait: true} if you'd like to wait for the server to respond before removing the model from the collection.

Params:

NameTypeAttributeDescription
options object

Provides optional properties used in destroying a model.

Return:

Promise | ParsePromise

Example:

book.destroy().then(() => {
   // do something
};

See:

public isNew(): boolean source

Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.

Return:

boolean

See:

public parse(resp: object, options: object): object source

parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model. This implementation requires a ParseObject and the attributes are directly taken from the attributes of the ParseObject. To keep parity with the Parse SDK the ID of the ParseObject is set as this.id.

Params:

NameTypeAttributeDescription
resp object

ParseObject

options object

May include options.parseObject.

Return:

object

Attributes from the ParseObject.

See:

public set(key: object | string, val: * | object, options: object): * source

Set a hash of attributes (one or many) on the model and potentially on the associated ParseObject. If any of the attributes change the model's state, a "change" event will be triggered on the model. Change events for specific attributes are also triggered, and you can bind to those as well, for example: change:title, and change:content. You may also pass individual keys and values. In addition option.updateParseObject may contain a boolean to indicate whether the associated ParseObject should be updated.

Params:

NameTypeAttributeDescription
key object | string

Either a string defining a key or a key / value hash.

val * | object

Either any type to store or the shifted options hash.

options object

Optional parameters.

Return:

*

Example:

note.set({ title: "March 20", content: "In his eyes she eclipses..." });

book.set("title", "A Scandal in Bohemia");

See:

public toJSON(): object source

Return a copy of the model's attributes object.

Return:

object

JSON representation of this model.

public url() source

This is an unsupported operation for backbone-parse-es6.