Skip to content

Instantly share code, notes, and snippets.

@khamusa
Created June 9, 2016 21:02
Show Gist options
  • Save khamusa/1f3816be48f3dd63808b7899956a13ee to your computer and use it in GitHub Desktop.
Save khamusa/1f3816be48f3dd63808b7899956a13ee to your computer and use it in GitHub Desktop.
Esboço de filtros para collections em js
define 'algumacoisa/filters', (), [] ->
class SomeFilteredCollectionCoordinator
initialize: ->
@filterGroups = [
new Filters.Group('has_rsvped', [
new Filters.Choice(when: false, selected: true),
new Filters.Choice(when: true)
]),
new Filters.Group('arrived_at', [
new Filters.Choice(with: _.isNull, when: false ),
new Filters.Choice(with: _.isNull, when: true, selected: true)
]),
new Filters.Group('invitee_gender', [
new Filters.Choice(when: 'male' ),
new Filters.Choice(when: 'female'),
new Filters.Choice(when: 'transgender')
])
]
new InvitationsCollection(filterGroups: @filterGroups)
define "helpers/filter", [], ->
class FilterGroup
matches : (model) ->
_.some @choices, (choice) -> # group filters with an "or" clause
!choice.selected? ||
choice.matches( model.get(@attributeName) )
class EqualityFilter
initialize: ({@when, @with, @selected}) ->
matches: (attributeValue) ->
@with(attributeValue) == @when
{
Group: FilterGroup,
Equality: EqualityFilter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment