The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Fun with Functions and CFCs VI - Mixin Method Methods

posted under category: ColdFusion on December 17, 2007 by Nathan

We have been playing with the concept of mixing in methods, or injecting them into CFCs voluntarily or not. Here is a list of ways you can dynamically assign methods to components.

Include method The easiest way to get common functions into multiple CFCs is to place them into a .cfm file and simply cfinclude the file. Place the cfinclude basically anywhere within your cfcomponent tags. The constructor area (anywhere not in a function) is the best place.

The greatness of the cfinclude mix-in method is that you set it basically in stone when you code it. It's a concrete thing and it doesn't change as it is executed, just like typing the functions into the CFCs themselves. I have used this for common setters, like a setUtilities method, where I wanted to inject it on a number of different unrelated objects, but didn't want to type it more than once, and it didn't make sense to extend a common component.

Cheap injection method This is one I covered previously - you can just set another function onto the public interface of a CFC. That function will now have access to the private variables of the CFC. You can, for example, make a getter for an internal variable that shouldn't have a getter. It's a cheap hack, and works great.

Sneaky injection I call this one private scope injection. Much like the above method, but you create a getVariables method and inject the function into the private scope.

The greatness of these two methods is that they are dynamic ways to hack into components. It feels dirty to me, but it's possible.

Single function mixin() method Somewhat more of an official way to do it would be to create a mixin function inside a CFC that takes a function name and function as arguments. The method will then set the function into the appropriate scopes. This is pretty clean, and hey, you can somehow inject the mixin() function itself.

Entire component mixin() method This is where mixins actually start to become useful. You can merge one CFC into another. Make a function that accepts a component as the argument, set the public "this" struct items to the current "this"scope and force some introspection to set the private variables into the current "variables" scope.

We'll talk more about this last one next time.

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.