ParseCollection
Extends:
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 |
clone(): Collection Returns a new instance of the collection with an identical list of models. |
|
public |
Delegates to |
|
public |
Delegates to |
|
public |
|
|
public |
Delegates to |
|
public |
Delegates to |
|
public |
whereBy(query: string, options: Object): Collection Delegates to |
Inherited Summary
From class Events | ||
public |
bind(): * Delegates to |
|
public |
Tell an object to listen to a particular event on an other object. |
|
public |
listenToOnce(obj: object, name: string, callback: function): Events Just like |
|
public |
Remove a previously-bound callback function from an object. |
|
public |
Bind a callback function to an object. |
|
public |
Just like |
|
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 |
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 a model (or an array of models) to the collection, firing an "add" event for each model, and an "update" event afterwards. |
|
public |
Get a model from a collection, specified by index. |
|
public |
clone(): Collection Returns a new instance of the collection with an identical list of models. |
|
public |
Convenience to create a new instance of a model within a collection. |
|
public |
Fetch the default set of models for this collection from the server, setting them on the collection when they arrive. |
|
public |
Just like |
|
public |
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 |
Override this method to specify the attribute the collection will use to refer to its models in collection.get. |
|
public |
|
|
public |
Pluck an attribute from each model in the collection. |
|
public |
Remove and return the last model from a collection. |
|
public |
Add a model at the end of a collection. |
|
public |
Remove a model (or an array of models) from the collection, and return them. |
|
public |
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 |
The set method performs a "smart" update of the collection with the passed list of models. |
|
public |
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 |
|
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 |
Return an array containing the attributes hash of each model (via toJSON) in the collection. |
|
public |
Add a model at the beginning of a collection. |
|
public |
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#constructorPublic Members
public comparator: string source
A comparator string indicating the attribute to sort.
Override:
Collection#comparatorpublic model: Model source
The default Backbone.Model class to use as a prototype for this collection.
Override:
Collection#modelPublic Methods
public clone(): Collection source
Returns a new instance of the collection with an identical list of models.
Override:
Collection#clonepublic find(query: string, options: Object): Array<Model> source
Delegates to BackboneQuery.find
to return an array of models that match the sort query.
public findOne(query: string): Model source
Delegates to BackboneQuery.findOne
to return the first model that matches the sort query.
Params:
Name | Type | Attribute | Description |
query | string | A query string. |
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#parsepublic 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:
Name | Type | Attribute | Description |
query | string | A query string. |
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.