Home Manual Reference Source Repository
import ParseCollection from 'backbone/src/ParseCollection.js'
public class | source

ParseCollection

You can directly use instance of this class. parseCollection

Extends:

EventsCollection → ParseCollection

Direct Subclass:

TodoList

ParseCollection - Collections are ordered sets of models. (http://backbonejs.org/#Collection)

This implementation of Backbone.Collection provides a parse method which coverts the response of a Parse.Query to ParseModels. One must set a Parse.Query instance as options.query or use a getter method such as "get query()".

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

In addition ParseCollection includes BackboneQuery support which supports local query / sorting of collections. Additional methods: find, findOne, resetQueryCache, sortAll, whereBy.

Example:


If using Backbone-ES6 by ES6 source one can create a module for a Backbone.Collection:
import Backbone   from 'backbone';
import Parse      from 'parse';

export default new Backbone.Collection(null,
{
   model: Backbone.Model.extend(...),
   query: new Parse.Query('<TABLE_NAME>')
});

or if importing a specific model class

import Backbone   from 'backbone';
import Parse      from 'parse';
import Model      from '<MY-BACKBONE-MODEL>'

export default new Backbone.Collection(null,
{
   model: Model,
   query: new Parse.Query('<TABLE_NAME>')
});

or use full ES6 style by using a getter for "model":

import Backbone   from 'backbone';
import Parse      from 'parse';
import Model      from '<MY-BACKBONE-MODEL>'

const s_QUERY = new Parse.Query('<TABLE_NAME>');

class MyCollection extends Backbone.Collection
{
   get model() { return Model; }
   get query() { return s_QUERY; }
}

export default new MyCollection();   // If desired drop "new" to export the class itself and not an instance.

Constructor Summary

Public Constructor
public

constructor(models: Array<Model>, options: object)

When creating a Collection, you may choose to pass in the initial array of models.

Member Summary

Public Members
public

A comparator string indicating the attribute to sort.

public

The default Backbone.Model class to use as a prototype for this collection.

public

query: Parse.Query

A Parse.Query instance

Method Summary

Public Methods
public

Returns a new instance of the collection with an identical list of models.

public

find(query: string, options: Object): Array<Model>

Delegates to BackboneQuery.find to return an array of models that match the sort query.

public

findOne(query: string): Model

Delegates to BackboneQuery.findOne to return the first model that matches the sort query.

public

parse(resp: object, options: object): object | Array[]

parse is called by Backbone whenever a collection's models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection. This implementation depends on parseSync which utilizes the Parse.Query attached to this collection to return a response of Parse.Object(s) which are then parsed into ParseModels.

public

Delegates to BackboneQuery.resetQueryCache to reset this collections query cache.

public

Delegates to BackboneQuery.sortAll to return all models that match the sort query.

public

whereBy(query: string, options: Object): Collection

Delegates to BackboneQuery.whereBy to return a new collection with the models that match the sort query.

Inherited Summary

From class Events
public

bind(): *

Delegates to on.

public

listenTo(obj: object, name: string, callback: function): Events

Tell an object to listen to a particular event on an other object.

public

listenToOnce(obj: object, name: string, callback: function): Events

Just like listenTo, but causes the bound callback to fire only once before being removed.

public

off(name: string, callback: function, context: object): Events

Remove a previously-bound callback function from an object.

public

on(name: string, callback: function, context: object): *

Bind a callback function to an object.

public

once(name: string, callback: function, context: object): *

Just like on, but causes the bound callback to fire only once before being removed.

public

stopListening(obj: object, name: string, callback: function): Events

Tell an object to stop listening to events.

public

Trigger callbacks for the given event, or space-delimited list of events.

public

unbind(): *

Delegates to off.

From class Collection
public

A comparator string indicating the attribute to sort.

public

The length of the models array.

public

The default Backbone.Model class to use as a prototype for this collection.

public

An array of models in the collection.

public

add(models: Model | Array<Model>, options: object): *

Add a model (or an array of models) to the collection, firing an "add" event for each model, and an "update" event afterwards.

public

at(index: number): *

Get a model from a collection, specified by index.

public

Returns a new instance of the collection with an identical list of models.

public

create(attrs: Model, options: object): *

Convenience to create a new instance of a model within a collection.

public

fetch(options: object): *

Fetch the default set of models for this collection from the server, setting them on the collection when they arrive.

public

findWhere(attrs: object): *

Just like where, but directly returns only the first model in the collection that matches the passed attributes.

public

get(obj: Model): *

Get a model from a collection, specified by an id, a cid, or by passing in a model.

public abstract

Initialize is an empty function by default.

public

modelId(attrs: object): *

Override this method to specify the attribute the collection will use to refer to its models in collection.get.

public

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

parse is called by Backbone whenever a collection's models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection. The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.

public

pluck(attr: string): *

Pluck an attribute from each model in the collection.

public

pop(options: object): *

Remove and return the last model from a collection.

public

push(model: Model, options: object): *

Add a model at the end of a collection.

public

remove(models: Model | Array<Model>, options: object): *

Remove a model (or an array of models) from the collection, and return them.

public

reset(models: Array<Model>, options: object): *

Adding and removing models one at a time is all well and good, but sometimes you have so many models to change that you'd rather just update the collection in bulk. Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event at the end. Returns the newly-set models. For convenience, within a "reset" event, the list of any previous models is available as options.previousModels. Pass null for models to empty your Collection with options.

Calling collection.reset() without passing any models as arguments will empty the entire collection.

Here's an example using reset to bootstrap a collection during initial page load, in a Rails application:

public

set(models: Array<Model>, options: object): *

The set method performs a "smart" update of the collection with the passed list of models.

public

shift(options: object): *

Remove and return the first model from a collection.

public

slice(): *

Return a shallow copy of this collection's models, using the same options as native Array#slice.

public

sort(options: object): Collection

Force a collection to re-sort itself.

public

sync(): *

Uses Backbone.sync to persist the state of a collection to the server.

public

toJSON(options: object): object

Return an array containing the attributes hash of each model (via toJSON) in the collection.

public

unshift(model: Model, options: object): *

Add a model at the beginning of a collection.

public

where(attrs: object, first: boolean): *

Return an array of all the models in a collection that match the passed attributes.

protected

_prepareModel(attrs: object, options: object): *

Prepare a hash of attributes (or other model) to be added to this collection.

protected

_reset()

Resets all internal state.

Public Constructors

public constructor(models: Array<Model>, options: object) source

When creating a Collection, you may choose to pass in the initial array of models. The collection's comparator may be included as an option. Passing false as the comparator option will prevent sorting. If you define an initialize function, it will be invoked when the collection is created. There are a couple of options that, if provided, are attached to the collection directly: model, comparator and query.

Pass null for models to create an empty Collection with options.

Override:

Collection#constructor

Params:

NameTypeAttributeDescription
models Array<Model>

An optional array of models to set.

options object

Optional parameters

See:

Public Members

public comparator: string source

A comparator string indicating the attribute to sort.

Override:

Collection#comparator

public model: Model source

The default Backbone.Model class to use as a prototype for this collection.

Override:

Collection#model

public query: Parse.Query source

A Parse.Query instance

Public Methods

public clone(): Collection source

Returns a new instance of the collection with an identical list of models.

Override:

Collection#clone

Return:

Collection

Returns a new collection with shared models.

See:

public find(query: string, options: Object): Array<Model> source

Delegates to BackboneQuery.find to return an array of models that match the sort query.

Params:

NameTypeAttributeDescription
query string

A query string.

options Object

Optional parameters

Return:

Array<Model>

public findOne(query: string): Model source

Delegates to BackboneQuery.findOne to return the first model that matches the sort query.

Params:

NameTypeAttributeDescription
query string

A query string.

Return:

Model

public parse(resp: object, options: object): object | Array[] source

parse is called by Backbone whenever a collection's models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection. This implementation depends on parseSync which utilizes the Parse.Query attached to this collection to return a response of Parse.Object(s) which are then parsed into ParseModels.

Override:

Collection#parse

Params:

NameTypeAttributeDescription
resp object

An array of Parse.Object(s).

options object

Unused optional parameters.

Return:

object | Array[]

An array or single ParseModel(s).

public resetQueryCache() source

Delegates to BackboneQuery.resetQueryCache to reset this collections query cache.

public sortAll(query: string): Array<Model> source

Delegates to BackboneQuery.sortAll to return all models that match the sort query.

Params:

NameTypeAttributeDescription
query string

A query string.

Return:

Array<Model>

public whereBy(query: string, options: Object): Collection source

Delegates to BackboneQuery.whereBy to return a new collection with the models that match the sort query.

Params:

NameTypeAttributeDescription
query string

A query string.

options Object

Optional parameters

Return:

Collection