mirror of https://github.com/freeCodeCamp/devdocs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
![]()
12 years ago
|
class app.Collection
|
||
|
constructor: (objects = []) ->
|
||
|
@reset objects
|
||
|
|
||
|
model: ->
|
||
|
app.models[@constructor.model]
|
||
|
|
||
|
reset: (objects = []) ->
|
||
|
@models = []
|
||
|
@add object for object in objects
|
||
|
return
|
||
|
|
||
|
add: (object) ->
|
||
|
if object instanceof app.Model
|
||
|
@models.push object
|
||
|
else if object instanceof Array
|
||
|
@add obj for obj in object
|
||
|
else if object instanceof app.Collection
|
||
|
@models.push object.all()...
|
||
|
else
|
||
|
@models.push new (@model())(object)
|
||
|
return
|
||
|
|
||
![]()
10 years ago
|
remove: (model) ->
|
||
|
@models.splice @models.indexOf(model), 1
|
||
|
return
|
||
|
|
||
![]()
12 years ago
|
size: ->
|
||
|
@models.length
|
||
|
|
||
|
isEmpty: ->
|
||
|
@models.length is 0
|
||
|
|
||
|
each: (fn) ->
|
||
|
fn(model) for model in @models
|
||
|
return
|
||
|
|
||
|
all: ->
|
||
|
@models
|
||
|
|
||
![]()
9 years ago
|
contains: (model) ->
|
||
|
@models.indexOf(model) >= 0
|
||
|
|
||
![]()
12 years ago
|
findBy: (attr, value) ->
|
||
|
for model in @models
|
||
|
return model if model[attr] is value
|
||
|
return
|
||
|
|
||
|
findAllBy: (attr, value) ->
|
||
|
model for model in @models when model[attr] is value
|
||
![]()
8 years ago
|
|
||
|
countAllBy: (attr, value) ->
|
||
|
i = 0
|
||
|
i += 1 for model in @models when model[attr] is value
|
||
|
i
|