Posted in ColdFusion | Posted on 12-22-2011 | 3,298 views
Hey, it's the holidays, so why not share one more quick ColdFusion Zeus update? This is another little feature that literally became available two days ago. If you've ever needed to dynamically invoke a CFC method, you know that's simply enough using cfinvoke. So for example:
2
3<cfset dynmeth = "test">
4<cfinvoke component="#s#" method="#dynmeth#" returnVariable="res">
5<cfdump var="#res#">
But of course, that's tag based. How do you do this in script? Unfortunately, the only way to do it was with evaluate, and while evaluate isn't as slow as it used to be, it can get messy to use.
ColdFusion Zeus corrects this by adding an invoke function.
2
3<cfset dynmeth = "test">
4
5<cfset res = invoke(s, dynmeth)>
6<cfdump var="#res#">
The first argument is either an instance of a CFC or the name of a CFC. The second argument is the method to call. If you need to pass arguments, then you can simply add a third argument - a struct.
2<cfoutput>#res#<p/></cfoutput>
By the way - take note of the fix to implicit struct notation. You can still use an equals sign ({x=4}), but colons work now too.
As a final quick aside - a certain person decided to take my words out of context regarding these Zeus previews I've been doing. That person also hasn't bothered to approve the comment I added 24 hours or so ago. I'm not going to bother linking to troll bait, but to be clear - these Zeus previews have intentionally focused on small things. It's easier for me to write, and secondly, it lets me ramp up slowly to the larger things we've announced already, like REST, closures, and web sockets. (Also, some of our larger features are still being hashed out.) For folks who attended my MAX preso, you already know this. Personally I'm really enjoying sharing these small, but useful, improvements, but I want folks to know that - obviously - we have bigger things in the works.


compObjStr = "com.whatever";
compObj = createObject("component" , compObjStr );
Gr
Yuri
As always, thanks for sharing about even the small enhancements coming in Zeus. I know the troll criticized the highlighting of these features, but for those of us who _actually_ like ColdFusion, this stuff is simply gold.
I look at it like this. I'm sure there are dozens and dozens of people who are waiting for X or Y *huge* feature to be implemented into CF. And I'm sure once X or Y is implemented, they will immediately use it to its full capability.
However, there are also a ton of us who use CF, will upgrade to Zeus when it's available, and will probably never use X or Y huge feature. But we will use the heck out of the fix for colons in implicit struct creation. We will use the new callstack capability. And we will use datetimeformat.
While all of these are certainly "small" in the grand scheme of all that CF does, can do, and will do, they also quickly add up for developers who use CF every day in the calculus of why we love using it, and why we anticipate what's to come in future versions.
Keep 'em coming!!
Can the new Invoke Method take a string as it's first value like how cfinvoke can take the name of a component for the component attribute? ex: invoke(component = "myComponent", method = "foo", arguments = {foo:'bar'});
If so, is this really the best thing to do? On each invoke it would be instantiating a temporary transient object that represents the string you passed in just so it can call the function on that object. The only use cases of passing a string that represents the object instead of the object itself would be utility components to fake static function calls. So wouldn't it make more sense to include the static modifier to components so you wouldn't even have to instantiate the object and have that overhead each time you called an invoke? Or further, wouldn't it make sense to add the ClassName.staticFunction() OR local.class = new ClassName; local.class.staticFunction() syntax to CF?
If not so, why is Invoke going to be a global function if the first argument is always a component? Wouldn't it make sense to make it a method on the component object? ex: myComponent.invoke(method = "foo", arguments = {foo:'bar'});
Yes, I said that. :) "The first argument is either an instance of a CFC or the name of a CFC."
"If so, is this really the best thing to do?"
It depends. Almost always folks make an instance of a CFC and keep it around. But not always. Point is - both is supported.
"If not so, why is Invoke going to be a global function if the first argument is always a component?"
If I had to guess, to ensure it wouldn't break with folks who used invoke as a method name.
"If I had to guess, to ensure it wouldn't break with folks who used invoke as a method name."
Good point.
Peter - yes. :)
s["sum"](x:4, y:9)
i know that Sean Corfield has porposed this, and this could be is a great step forward for the language!!!
regards
I'm also VERY glad to see that implicit structure notation accepts COLONS as name/value pair delimiters. Since I do so much JS/Json work now I can't tell you how many times I have to go back and replace my equals signs with colons.
I can't wait for CF10 to come out!
It'll be nice to just use colons on both sides.
does this function only support cfc invocation or also webservice invocation like the tag does?
if webservice call are supported how this is working without named arguments?
tnx markus
And it'll mean ACF10 behaves like the other CFML engines have done for several years :)
And I can see where it's come from, given how many other dynamic languages have some sort of "invoke" method on a metaprogramming object...
[Add Comment] [Subscribe to Comments]