API Docs for: 0.0.6

Class Protobone.Collection

Extends: Protobone.Base
Class defined in: src/Collection.js:1

PrototypeJS Model extension - Enables Prototype JS users to fetch / store Models from / to a backend using AJAX / REST. The Collection class stores Models and allows storing / fetching them in a batch.

Inspired by (but not copied) Backbone's Backbone.Collection and Backbone.sync

Protobone.Collection( )
src/Collection.js:1
add( {Object} )
src/Collection.js:72

The add method takes a Model or an array of Models and adds them to the internal collection. Plain objects can also be delivered, which will be transformed to Prototype.Model instances or, if set, to models of the this.model class.

Parameters:
  • {Object} <Array>

    data The array / object of model(s) to be added

Model at( index )
src/Collection.js:169

returns the model at the given index, or null if index is out of bounds

Parameters:
  • index <Integer>

    The index to fetch the model for


Returns: <Model>

The model at the index or null

constructor( data )
src/Collection.js:39

Constructor. A data array can be delivered to create Models already during construction time (e.g. [{name: 'alex'},{name: 'barbara'}])

Parameters:
  • data <Array>

    Initial data (array of key/value pairs) to create Model's from

Array filter( predicate )
src/Collection.js:234

Returns all models matching a given predicate function. The predicate function gets the actual model, and must return true if it matches, or false if not. Example:

var c = new Protobone.Collection([
    {author: 'Stephen King', title: 'Carrie'},
    {author: 'Stephen King', title: 'Needful Things'},
    {author: 'Jane Austen', title: 'Persuasion'},
]);
var res = c.filter(function(item){
    return item.get('author') === 'Stephen King';
});
// => returns all Models with Stephen King as author
Parameters:
  • predicate <Function>

    The predicate function as test for each model


Returns: <Array>

The matching models

findBy( attribute, value, Returns )
src/Collection.js:114

Find a model by a specific attribute value.

Parameters:
  • attribute <String>

    The name of the attribute

  • value <Mixed>

    The value of the attribute

  • Returns <Model>

    the first model with a matching attribute value, or null if none can be found

Model findWhere( predicateProps )
src/Collection.js:128

Like where,

Parameters:
  • predicateProps <Object>

    A key/value hash with attributes/values to match


Returns: <Model>

The first matching model

Boolean fireEvent( eventName )
Defined in Protobone.Base: src/Base.js:60

Fires an event, informing all listneners that are registered for the given event name. The fireEvent function can be called with any number of additional arguments, which are then passed to the event handler function.

Returns true if NONE of the registered handlers return false: As soon as one listener returns false, fireEvent will also return false.

Parameters:
  • eventName <String>

    The event to fire, e.g. 'updated'


Returns: <Boolean>

true when non of the listeners returned false, false if they do so.

forEach( callback )
src/Collection.js:192

loops over all models and calls the given callback with callback(model,index). Return false within the callback to cancel the loop.

Parameters:
  • callback <Function>

    The callback for every item to be called. Return false to cancel the loop

Model get( id )
src/Collection.js:102

Returns the model by specific ID

Parameters:
  • id <Mixed>

    The ID of the model


Returns: <Model>

The model if found, or null

Integer indexOf( model )
src/Collection.js:181

returns the index of the given model, or -1 if the model is not in the collection.

Parameters:
  • model <Model>

    The model to find


Returns: <Integer>

The index, or -1 if not in collection

off( eventName, callback, this )
Defined in Protobone.Base: src/Base.js:34

Removes a specific event handler for an event, or removes all listerners from an event.

Parameters:
  • eventName <String>

    E.g. 'updated'

  • callback <Function>

    The callback to remove. If omitted, all callbacks for a specific event are removed

  • this <Boolean>
on( eventName, callback, this )
Defined in Protobone.Base: src/Base.js:16

Registers an event handler. It does not check on duplicity, so you can add the same event handler multiple times.

Parameters:
  • eventName <String>

    The name of the event, e.g. 'updated'

  • callback <Function>

    The listener function, called with event-specific {parameters}

  • this <Boolean>
push( Same )
src/Collection.js:158

alias for add(), to support "array like" behaviour

Parameters:
  • Same <Object>

    as add, data The array / object of model(s) to be added

remove( The )
src/Collection.js:264

Removes the given model(s) (single model or array of models) from the collection. fires a remove event. If the model occurs multiple times, all of them are removed.

Parameters:
  • The <Model/Array>

    model(s) to be removed

remove( The )
src/Collection.js:291

Removes the model at the index given.

Parameters:
  • The <Integer>

    index of the model to be removed

Array where( predicateProps )
src/Collection.js:208

Simple application of filter() function: returns all models matching the given attributes, e.g.:

var c = new Protobone.Collection([
    {author: 'Stephen King', title: 'Carrie'},
    {author: 'Stephen King', title: 'Needful Things'},
    {author: 'Jane Austen', title: 'Persuasion'},
]);
var res = c.where({author: 'Stephen King'});
// => returns all Models with Stephen King as author

Multiple attributes are allowed.

Parameters:
  • predicateProps <Object>

    A key/value hash with attributes/values to match


Returns: <Array>

The matching models

model <Class>
src/Collection.js:31

Defines the model class used when adding models via attribute array.

urlRoot <String>
src/Collection.js:20

The URL root for this Collection. Must be set in child classes, e.g. to '/entities/Person', or must be defined in the defined Model.

Used by the url() function to build the persistence URL.