More of just a plain newbie than a PHP-developer ;) Got hooked on CF
straight out of the university ("Hey, this is *way* easier than JSP's!") and
been developing in relative isolation for the past two years. This mailing
list alone has shown me the light more often than the Bible ;)
Anyway, back to the topic!
>> As for including a UDF cfm file
>> in your cfc - this is a great idea,
>> but I am interested in why you're
>> going out on such a ledge, even if
>> it's just because you've done enough
>> PHP to warp your brain ;)
I'm building an application generator of sorts. I'm storing the database
schema data (Entities and their Fields) in CFC's (one for each Entity).
Those CFC's contain generic CRUD DAO methods, and also a few other
miscellaneous ones. I got the basics running, but now I'm to the part of
adding Relationships to the CFC's. These relationships are dependencies
that I'd like to have managed by ColdSpring -- ColdSpring looks for the
set******() method for any dependencies, so I need to make sure that
function exists in my CFC!
If I'm attacking this problem wrong, feel free to shoot me straight -- I'll
cough up some source code if you want to see exactly what's going on.
There's not too much in it just yet, but I've already found it oh-so-useful,
and if I could have it handle my schema relationships too, I'll be ecstatic.
:)
Jonathon
-----Original Message-----
From: Nathan Strutz [mailto:[EMAIL PROTECTED]
Sent: Monday, October 22, 2007 4:23 PM
To: CF-Talk
Subject: Re: programmatically add functions to a CFC
Jonathon,
PHP guy eh? In CF there's no way to have a function in a function. Same with
Java. Generally speaking, make another object. Or there's always passing
functions as arguments. See, what it sounds like is you're coming at the
problem backwards, however, that's not necessarily a bad thing. How about
some functional programming?
function add(n1,n2) {return n1+n2;}
function subtract(n1,n2) {return n1-n2;}
function doSomethingWith2Numbers(n1,n2, thingToDo) { return
thingToDo(n1,n2);}
writeOutput( doSomethingWith2Numbers( 5, 5, add) ); writeOutput(
doSomethingWith2Numbers( 5, 5, subtract) );
(even runs on 6.1)
Now, like I said, this is backwards from most CF, but totally allowable by
the language.
As for including a UDF cfm file in your cfc - this is a great idea, but I am
interested in why you're going out on such a ledge, even if it's just
because you've done enough PHP to warp your brain ;)
--
nathan strutz
http://www.dopefly.com/
On 10/22/07, Jonathon Stierman <[EMAIL PROTECTED]> wrote:
>
> That is the crucial key I needed! Much easier than what I had tried to do
> with the meta data too -- bonus!
>
> >like a cfinclude file (even with a dynamic file name) from inside the CFC
>
> Is this to get around the inability to nest <cffunction> tags in CFC's?
> That's definitely a hurdle... any speculation as to why CF doesn't offer
> us
> a Function object we could create and modify, without having to use
> CF-tags
> or cfscript? Or does it? I'd prefer to create a blank function object,
> then fill it up with the details... Hmm. Just how would I set the
> functionality of the function without a cffunction tag. I'd almost
> need...
> another function :P Well, alright, that's a little trickier than I had
> first thought.
>
> So it seems accepted practice to include a .cfm file from your component
> to
> get around that. Anyone care to play devils advocate against that
> practice?
> If I'm dynamically writing the .cfm file content (which I will be doing),
> I
> then just include that .cfm file within my function and reference my
> function (stored as a variable) from there. Interesting...
>
> Am I still on track, or wildly flailing in the wind? :)
>
> Jonathon
>
> -----Original Message-----
> From: Nathan Strutz [mailto:[EMAIL PROTECTED]
> Sent: Monday, October 22, 2007 1:16 PM
> To: CF-Talk
> Subject: Re: programmatically add functions to a CFC
>
> Jonathon,
>
> You're on the right track, but it's much easier than that. Here's some
> play
> code I just whipped up:
>
> <cfscript>
> function getVariablesExternal() {return variables;}
> function methodToInject() {return "This method was Injected";}
> myCFC = createObject("component","test").init(); // get component
> myCFC.getVariables = getVariablesExternal; // inject variable scope
> getter
> into "this" scope
> myCFC.getVariables().injectedMethod = methodToInject; // inject a method
> into CFC "variables" scope
> myCFC.getVariables().this.injectedMethod = methodToInject; // also into
> public "this" scope
> writeOutput( myCFC.getVariables().privateMethod() ); // call private
> method from test.cfc
> writeOutput( myCFC.injectedMethod() ); // call injected method
> </cfscript>
>
> It's really amazing the things you can do with this stuff :D
>
> PS here's my test.cfc:
>
> <cfcomponent>
>
> <cffunction name="init" access="public">
> <cfreturn this>
> </cffunction>
>
> <cffunction name="privateMethod" access="private">
> <cfreturn "This is a Private Method!">
> </cffunction>
>
> <cffunction name="publicMethod" access="public">
> <cfreturn "This is a Public Method!">
> </cffunction>
>
> </cfcomponent>
>
>
> This is actually the concept of mix-ins, there has been some more formal
> code written about it, but you can do it like this, like a cfinclude file
> (even with a dynamic file name) from inside the CFC, you can get a struct
> of
> functions and loop over them, calling each or injecting them elsewhere,
> you
> can have a mixin() method that takes a function as an argument, and so on
> and so on. Really, lots of fun things here!
>
> --
> nathan strutz
> http://www.dopefly.com/
>
>
> On 10/22/07, Jonathon Stierman <[EMAIL PROTECTED]> wrote:
> >
> > Is it possible to programmatically add/update functions to an
> instantiated
> > CFC?
> >
> > I've made one attempt to add one by calling the getMetaData function,
> but
> > nothing I added to the meta data seemed to update the object's method
> > list.
> >
> > Is there another way to go about doing this, or am I on the right track?
> >
> > Jonathon
> >
> >
> >
> >
>
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
ColdFusion 8 - Build next generation apps
today, with easy PDF and Ajax features - download now
http://download.macromedia.com/pub/labs/coldfusion/cf8_beta_whatsnew_052907.pdf
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:291792
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4