mixins in cfcs

83 views
Skip to first unread message

Robert Munn

unread,
Mar 21, 2015, 4:25:48 AM3/21/15
to lu...@googlegroups.com
I have a Coldbox app and I want to inject a common set of methods into my handlers. I was thinking of using a mixin approach, and I was re-reading Sean Corfield’s post about mixins:


I was wondering what people thought of the idea of modifying the base Component class to add the mixin function, and if this is a candidate for the kind of functionality that might be standard in a new Lucee version. Since I am only need the functionality for a subset of components, I don’t necessarily want to add the method to every component I instantiate, but the alternative is more or less to copy what Sean’s function does inline or in a base class. 

Here is Sean’s function: 

     function mixin(type) {
         var target = createObject("component",arguments.type);
         structAppend(this,target);
         structAppend(variables,target);
      }

I suppose I could extend coldbox.system.EventHandler to a base handler class of my own so I can add the mixin method there. I’d rather not, though. Other ideas?

Robert

Dominic Watson

unread,
Mar 21, 2015, 8:43:38 AM3/21/15
to lu...@googlegroups.com
You might want to take a look at "Virtual Inheritance" in Wirebox: http://wiki.coldbox.org/wiki/WireBox.cfm#Virtual_Inheritance

I had missed this before when searching for "mixins".

HTH

--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/9B44D0F3-060E-4384-9D1C-A0D10AEBA640%40gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Pixl8 Interactive, 3 Tun Yard, Peardon Street, London
SW8 3HT, United Kingdom

T: +44 [0] 845 260 0726 W: www.pixl8.co.uk E: in...@pixl8.co.uk

Follow us on: Facebook Twitter LinkedIn
CONFIDENTIAL AND PRIVILEGED - This e-mail and any attachment is intended solely for the addressee, is strictly confidential and may also be subject to legal, professional or other privilege or may be protected by work product immunity or other legal rules. If you are not the addressee please do not read, print, re-transmit, store or act in reliance on it or any attachments. Instead, please email it back to the sender and then immediately permanently delete it. Pixl8 Interactive Ltd Registered in England. Registered number: 04336501. Registered office: 8 Spur Road, Cosham, Portsmouth, Hampshire, PO6 3EB

Brad Wood

unread,
Mar 21, 2015, 11:24:46 AM3/21/15
to lu...@googlegroups.com
Hi Robert, you don't need to worry about any of those online approaches.  You're using ColdBox so we've already given you solutions for adding mixins out-of-the-box :)

Your first option is the applicationHelper setting (Name changed from UDFHelper in ColdBox 3.x).  


applicationHelper
A list or array of absolute or relative paths to a UDF helper file. The framework will load all the methods found in this helper file globally. Meaning it will be injected in ALL handlers, layouts and views.

In /config/ColdBox.cfc
coldbox={
    applicationHelper             = "includes/helpers/ApplicationHelper.cfm"
}
You second option is a bit more laser and allows you to add mixins on an individual CFC basis.  You can give WireBox a list of files to include UDFs from into your components. 

If you like the WireBox DSL for mapping your components it would look like this in your /config/WireBox.cfc:

map("MyService")
	.to("model.UserService")
	.mixins("/helpers/base");

My favorite WireBox approach though is to keep the config for a CFC right there inside the component.  That's why we also have the annotation approach for this feature:

component mixins="/helpers/base"{

}

Thanks!

~Brad

Robert Munn

unread,
Mar 22, 2015, 3:22:39 AM3/22/15
to lu...@googlegroups.com
Interesting, but I don’t think that approach will work in this case because I am adding functionality to handlers, which are not listed in Wirebox. 

Dominic Watson

unread,
Mar 22, 2015, 3:25:36 AM3/22/15
to lu...@googlegroups.com
> Interesting, but I don’t think that approach will work in this case because I am adding functionality to handlers, which are not listed in Wirebox.

Handlers are managed through WireBox. Did you try any of those approaches?




For more options, visit https://groups.google.com/d/optout.

Brad Wood

unread,
Mar 22, 2015, 10:50:07 AM3/22/15
to lu...@googlegroups.com
Handlers are still created by WireBox. You should be able to just add the "mixins" annotation to the CFC and it will pick it up.  I *think* you can also create an explicit mapping named after the handlers full path (handlers.myHandler) but I'd need to test that real quick to confirm.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


--
You received this message because you are subscribed to a topic in the Google Groups "Lucee" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lucee/m5Goy9azOe0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lucee+un...@googlegroups.com.

To post to this group, send email to lu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages