Class: Wraith.Collection

Defined in: src/collection.coffee
Inherits: Wraith.Base

Overview

The Collection object is a collection of Wraith.Model's that are accessed through Credit goes out to Alex MacCaw and his Spine framework for very obvious inspiration.

Instance Method Summary

Inherited Method Summary

Methods inherited from Wraith.Base

#constructor, #bind, #unbind, #emit, #proxy

Constructor Details

- (void) constructor(parent, as, klass)

Sets up the parent, namespace (@as) and Wraith.Model class.

Parameters:

  • parent (Wraith.Model) The parent model of this collection.
  • as (String) The name space that this collection is stored as on the parent.
  • klass (Wraith.Model) The Wraith.Model that is used to create each item in the collection.

Instance Method Details

- (Wraith.Model) create(attr) (bound)

Creates a new instance of the Wraith.Model belonging to this collection.

Parameters:

  • attr (Object) The attributes object used to initialize the model.

Returns:

- (Wraith.Model) add(item) (bound)

Adds the given instance of a Wraith.Model to the collection.

Parameters:

Returns:

  • (Wraith.Model) — The item that was added to the collection (same as what was passed in)

- (Wraith.Model, Boolean) remove(id) (bound)

Removes a model from the collection, by ID (the _id attribute).

Parameters:

Returns:

  • (Wraith.Model, Boolean) — The item that was removed from the collection or false if not found.

- (Array<Wraith.Model>) all() (bound)

Returns all members of the collection as an array.

Returns:

- (Number) length() (bound)

Returns the length of the members array.

Returns:

  • (Number) — The length of the members array (0 or greater)

- (Wraith.Model) findById(id) (bound)

Finds a given model within the collection by id (_id property)

Parameters:

  • id (String) The id to search for

Returns:

- (void) handleChange(key, value) (bound)

Acts as a wrapper for handling the change function coming from a model within the collection. This emits a change event from the parent whenever a change happens on a model.

Parameters:

  • key (String) The key of the changed model value
  • value (String, Object, Boolean) The changed value itself