Declarative event binding library
If you are writing your own GUI components, or you are just tired of event-binding boilerplate, this package is what you need. Out of the box:
- Declarative event binding, with concise format, very clear about event source, name and its handlers
- Event handling logic is now a data structure (!!), not code
- Event handling inheritance
- Event handling composition
- Light-weight event emission (no built-in bubble/capture), but with arguments
# I use different parentheses styles to separate method calls from function calls:objmethodfunction1 function2 arg1arg2# I don't use brackets on methods, if I pass anonymous multiline functions.$jquery_elementon 'click'consolelog'wow'
# Your old boilerplate to bind handlers to events:: ->$some_jqueryon 'click'task1etask2etask3e@non_jquery_emitteron 'server_event'if event_is_valid e@update_viewe;# Your new boilerplateevent_table:$some_jquery 'click' task1task2task3'non_jquery_emitter' 'server_event' event_is_valid'update_view'bind_events
Clone the repo, open demo/index.html in browser, move your mouse, click on blocks, look at console output.
Read the demo/app.coffee it's very short.
Create emitter class, describe its emitters, events and reactions
# [ [ emitter_name, [ [ event_name, [ reactions... ] ] ] ] ]# let me show it trough simple composition# I will write a class descibing a brain of an ancient human,# That should be able to handle various events.# Eventsevent_table:'ear' 'snake_heard' 'emit_adrenaline''look_around''eye' 'food_spotted' 'emit_noradrenaline''hunt''emit_endorphins''predator_spotted' 'emit_cortisol''emit_adrenaline''run''nose' 'food_smelled' 'look_around''blood_smelled' 'emit_adrenaline''look_around'# constructor and instance members= ->@ear =@eye =@nose =# methods:: ->: ->: ->: ->: -># The event table can be decomposed as followingflee_reactions = 'emit_cortisol''emit_adrenaline''run'hunt_reactions = 'emit_noradrenaline''hunt''emit_endorphins'seek_reactions = 'look_around'watch_outs = 'emit_adrenaline''look_around'ear_events = 'snake_heard' watch_outseye_events = 'food_spotted' hunt_reactions'predator_spotted'flee_reactionsnose_events = 'food_smelled' seek_reactions'blood_smelled' watch_outsear_pack = 'ear' ear_eventseye_pack = 'eye' eye_eventsnose_pack = 'nose'nose_eventsBrain::event_table = ear_packeye_packnose_pack
# Use in a method# ... event table and stuff ...# simplest form: ->@emit'adrenaline'# or with arguments:@emit'adrenaline'dose_mldelay_msnoradrenaline# square brackets used to denote arguments from event name
# the ones called without args: ->consolelog argumentslength # -> 0# the ones called with args:consolelog arguments
And you can do with it anything you can do with data. Equality, cloning and merging.
There is a
class_tools module, which contains
function. It is a pure function merges two or more event tables
into one. New items will be added, repeating ones will be
table1 = 'nose' 'smell-cheese' 'look_around''eye' 'sought-food' 'grab_item'table2 = 'nose' 'smell-cheese' 'search_food''eye' 'sought-food' 'grab_item''chew_item'class_toolsmerge_events table1table2 # -> will produce:'nose' 'smell-cheese' 'look_around''search_food''eye' 'sought-food' 'grab_item''chew_item'