You can listen for any events and emit them like in nodejs
//require event_camelconstevent_camel=require("event_camel")//my camel objectconstcamel=newevent_camel()//Adding events and their handlerscamel.On("my-event",function(name){console.log(name)})camel.On("calc",function(number,num2){console.log(Math.pow(number,num2))})//over write an exsiting eventcamel.On("my-event",function(name){console.log("Hi we over write priviose event ",name)})//Emiting Eventscamel.Emit("my-event","khattab")camel.Emit("calc",12,2)//error: "undefined event"camel.Emit("some undefiend event",12)
listing for Dom Events
You can play with dom events and change them with your handlers
make sure that you are using the client side version on dist/event_camel.js
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible" content="IE=edge"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>EventCamel-browser example</title><scriptsrc="./path/to/event_camel.js"></script></head><body><divid="counter">0</div><buttonclass="btn" id="inc-btn">Increment</button><buttonclass="btn" id="dec-btn">Decrement</button><script>constcamel=newEventCamel()varcounter=document.getElementById("counter")//get Increment and decremnt buttonsvarinc_btn=document.getElementById("inc-btn")vardec_btn=document.getElementById("dec-btn")//setting main events "decremnt" and "increment"camel.On("increment",function(payload){counter.innerText=parseInt(counter.innerText)+payload})camel.On("decrement",function(payload){counter.innerText=parseInt(counter.innerText)-payload})//buttons listenersinc_btn.addEventListener("click",function(){camel.Emit("increment",1)})dec_btn.addEventListener("click",function(){camel.Emit("decrement",1)})</script></body></html>
It looks like the double work but in fact It can very help full at orgnize your code accross many javascript files
for the full styled example check dist folder.
Emit API
rich API that allows you to emit events easly in diffirent situtaions
constcamel=newevent_camel()camel.On("more-than-100",function(event){console.log("helle from event "+event)})camel.On("less-than-100",function(event){console.log("helle from event "+event)})camel.On("green",function(event){console.log("helle from event "+event)})camel.On("red",function(event){console.log("helle from event "+event)})camel.On("yellow",function(event){console.log("helle from event "+event)})//Emit single Eventcamel.Emit("yellow")//Emit Many Eventscamel.EmitMany(["yellow","red","green"])//Emit All Eventscamel.EmitAll()//Emit when the condition is equal to resultconstnum=99constcondition=num<100constif_true=trueconstif_false=falsecamel.EmitIf("less-than-100",condition,if_true)camel.EmitIf("more-than-100",condition,if_false)
Single-Events
Single Events are basicly one Event Generated by CamelEvent function
It is recomended to use EventCamel instance in large applications but you have the Choice
constMyEvent=CamelEvent("event1",function(event){console.log("hello from single event")},{trace:true,trace_func:function(event){console.log("traking.."+event)}})//trace function is going to run beforMyEvent.Emit()
You might ask your self why I need to define event_name again? The answer is that in most times you are going to deal whith many events at the time.
There is a function called CamelEventsTree to help you deal with many single events
constorder_car=CamelEvent("order-car",function(event){console.log("This is single event for ordering "+event)})constorder_pizza=CamelEvent("order-pizza",function(event){console.log("this is single event for ordering "+event)})constorder_halwa=CamelEvent("order-halwa",function(event){console.log("this is single event for ordering"+event)})constevents_tree=CamelEventsTree([order_car,order_pizza])//You Can Add The one by one if yu likeevents_tree.addEvent(order_halwa)events_tree.EmitOne("order-halwa")events_tree.EmitOne("order-pizza")
The reason Why it is not recomended because events_tree.EmitOne() Find the frist match based one event name
so maybe if you duplicate events_names it going to case problmes in production
just use EventCamel instance and you are Fine with unique events handling