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.
devdocs/assets/javascripts/collections/collection.coffee

56 lines
1.1 KiB

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
remove: (model) ->
@models.splice @models.indexOf(model), 1
return
size: ->
@models.length
isEmpty: ->
@models.length is 0
each: (fn) ->
fn(model) for model in @models
return
all: ->
@models
contains: (model) ->
@models.indexOf(model) >= 0
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
countAllBy: (attr, value) ->
i = 0
i += 1 for model in @models when model[attr] is value
i