FORGEBOX Enterprise 🚀 - Take your ColdFusion (CFML) Development to Modern Times! Learn More...

cfEvents

v0.2.0 Public

cfEvents

Bitbucket Pipelines build status issues

Make your native CFML app extendable by defining custom events, listening for those events, and running a closure when the event occurs. cfEvents is similar to the interceptors provided in Coldbox, only with less configuration required and fewer features. :)

Getting Started

box install cfevents

Then include cfevents in some global scope.

request.cfEvents = new cfevents.models.cfEvents();

Listening for Events

Pass an event name and a closure in to the listen() method to listen for an event. The closure will get executed any time that cfevents.run() is called with that event name.

request.cfEvents.listen("htmlHead", function() {
	// stick this file in the head
	writeOutput('<invalidTag src="/dist/app.js"></script>');
});

Running Events

You can run an event by calling cfEvents.run('eventName'). This will execute every closure associated with that event in order of creation.

request.cfEvents.run("htmlHead");

You can pass arguments to the executing closure by providing them as a struct.

request.cfEvents.listen("formSubmit", function( required component mailer, required struct data ) {
	// email form to someone
	cfmail(to = "[email protected]") {
		writeOutput('<p>#arguments.data.name# sent you a message:</p>');
		writeOutput('<p>#arguments.data.comments#</p>');
	}
});
request.cfEvents.run("formSubmit", { "mailer": mailerObj, "data": form });

You can even pass an object with a method name to run on event execution:

var paypal = new paypal.paypal();
// "chargePaypal" function in paypal object will take the form parameters and send a payment charge.
request.cfEvents.listen( "formSubmit", "chargePaypal", paypal );
...
request.cfEvents.run( "formSubmit", form);

Variable Scoping in Events

First, remember that a closure will inherit the local scope of the defining block (e.g. local and variables scope outside the closure definition are available inside the closure).

Secondly, this inside an event closure will refer to the cfEvents class. So instead of calling component methods via this.myFunc(), use variables.myFunc() or just myFunc() if you don't care about scoping your vars. (I recommend scoping!)

Engine Support

Sorry - this is currently Lucee only, due to the use of parallel ArrayEach().

  • Lucee 4.5+

TODO

  • ✅ Execute events asynchronously
  • ✅ Pass component and method to listen() method in addition to closures.
  • â�Œ Add WP-like priority execution - e.g. higher priority closures get executed first.
  • â�Œ Add WP-like "filters" or similar to support return values.

The Good News

For all have sinned, and come short of the glory of God (Romans 3:23)

But God commendeth his love toward us, in that, while we were yet sinners, Christ died for us. (Romans 5:8)

That if thou shalt confess with thy mouth the Lord Jesus, and shalt believe in thine heart that God hath raised him from the dead, thou shalt be saved. (Romans 10:9)

Repository

Copyright 2019 (and on) - Michael Born

cfmlbadges cfmlbadges cfmlbadges cfmlbadges

Here are all the versions for this package. Please note that you can leverage CommandBox package versioning to install any package you like. Please refer to our managing package version guide for more information.

Version Created Last Update Published By Stable Actions
Current
0.2.0 Jul 13 2019 09:53 PM Jul 13 2019 09:53 PM
Version History
0.1.1 Jun 20 2019 11:42 PM Jun 20 2019 11:42 PM
0.1.0 Jun 20 2019 11:33 PM Jun 20 2019 11:33 PM

 

No collaborators yet.
  • Jun 20 2019 11:33 PM
  • Jul 13 2019 09:53 PM
  • 77
  • 39
  • 1