Home Manual Reference Source Repository
public class | source

TodoList

You can directly use instance of this class. todoList

Extends:

EventsCollectionParseCollection → TodoList

This module defines a Backbone.Collection which stores todos Items and provides utility methods to filter, sort, and retrieve the next order number used to sort Items. Instead of exporting the class itself an instance of the the class is exported. This allows the instance to be imported anywhere access to the collection is necessary. Multiple views may access this instance, but in this demo only ManageTodosView displays data from the collection.

Please note that the Parse.Query is generated in App.js during user login using the current user ID to retrieve Items associated with the current user.

Member Summary

Public Members
public get

model: Item: *

Reference to this collection's model.

Method Summary

Public Methods
public

Todos are sorted by their original insertion order.

public

done(): *

Filter down the list of all todos items that are finished.

public

We keep the todos Items in sequential order, despite being saved by unordered GUID in the database.

public

remaining(): *

Filter down the list to only todos items that are still not finished.

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.

From class ParseCollection
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

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.

Public Members

public get model: Item: * source

Reference to this collection's model.

Override:

ParseCollection#model

Return:

Item

Public Methods

public comparator(item: Item): number source

Todos are sorted by their original insertion order.

Override:

ParseCollection#comparator

Params:

NameTypeAttributeDescription
item Item

item model.

Return:

number

public done(): * source

Filter down the list of all todos items that are finished.

Return:

*

public nextOrder(): number source

We keep the todos Items in sequential order, despite being saved by unordered GUID in the database. This generates the next order number for new items.

Return:

number

public remaining(): * source

Filter down the list to only todos items that are still not finished.

Return:

*