Wednesday 6 November 2013

<cfclient>: worrying discussion

The more I read about <cfclient>, the more the alarms bells change from tending towards air raid sirens, and more like the horns of the Rapture (or if you prefer). Here's a series of comments on the Adobe ColdFusion Blog about how looping over arrays will "work" in <cfclient>.

Thanks to existdissolve for pursuing this line of questioning:

10 (existdissolve / ED):

Actually, now that I think about it, what about from/to loops?

Ex: <cfloop from="1" to="#arrayLen( someArray )#" index="x">...</cfloop>

Does CFClient translate the "1" to "0", or do a <= in the JS for() notation?

A: for( var x=0; x<10; x++ ) {} // I would do this in JS normally
B: for( var x=1; x<=10; x++ ) {} // I would not normally do this is JS
12 (Ram Kulkarni / RK):
@existdissolve, yes the framework translates 1 based array index (of CFML) to 0 based in JS. So in your example, it would generate code like in option A
13 (ED):


Thanks for the clarification. But does it always convert to 0-based? What if you need a non-zero-based for-loop in JS?
 14 (RK):
@existdissolve, even if you want to start a loop from somewhere in the middle of an array, array elements will have to be translated with zero based indices in JS. The same way we handle it on the server side when converting CFML to Java, where arrays are zero index based. Loop will work on the client side exactly the same way it works on the server side.
15 (ED):

Right, I get that. But what if I'm not looping over an array? Is it still going to convert to 0-based? That seems incorrect.

// loop from 1 to 9, printing 1,2,3,4,5,6,7,8,9
for( var i=1; i<10; i++ ) {
// something "i" here

If it automatically converts ALL for loops to 0-based, this breaks loops that aren't related to
18 (RK):

@existdissolve, if you are not accessing array elements in loop then we don't touch the variable. As I said, loop works exactly the same way on the client side as it does on the server side.
20 (Sean Corfield / SC):

@Ram, just so I'm clear on what you're saying to @existdissolve:

<cfloop index=i from=1 to=10>
<cfset a[i] = i>

This will create JS that does:

for (i=0; i<=10; i++) {
a[i-1] = i;

Is that correct?

Also, I see everyone talking about tag syntax - does cfclient support cfscript or just tags?

21 (RK):
@Sean - no, we won't change the value of index in the for loop, but would decrement array index.

for (i = 1; i <= 10; i++) {
a[i-1] = i;

And yes, you can write cfscript inside cfclient. Client side CFCs can also be written in complete script syntax.
23 (SC):
Thanx Ram. I'll be interested to see how this is received when the public beta becomes available.

I'll honest, I still think it's a _terrible_ idea. I think providing all the functionality as a pure JS library and documentation about the integration would have been a much better approach than this weird mix of CFML and JS.

How come - at some stage during that exchange - Ram didn't conclude "actually, this is all just frickin' stupid. What the hell have we been thinking?"

This is after - same thread - it was revealed that one cannot use named arguments in function calls within <cfclient> code.

How long is the list of language caveats going to end up being?