Class Protobone.Collection
Extends: Protobone.BaseClass 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
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.
-
{Object}
<Array>data The array / object of model(s) to be added
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
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'}]
)
-
data
<Array>Initial data (array of key/value pairs) to create Model's from
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
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
findWhere
( predicateProps
)
src/Collection.js:128
Like where,
Parameters:-
predicateProps
<Object>A key/value hash with attributes/values to match
The first matching model
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'
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.
-
callback
<Function>The callback for every item to be called. Return false to cancel the loop
get
( id
)
src/Collection.js:102
Returns the model by specific ID
Parameters:-
id
<Mixed>The ID of the model
The model if found, or null
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
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: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: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
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
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.