You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Bernardo Arlandis Mañó <be...@ya.com> on 2004/05/28 13:29:31 UTC

Generating javascript code with velocity

Hi. I've been using turbine for a while and i've been using velocity for 
generating html and javascript code inside .vm files, but now I'd like 
to take the javascript code out to its own .js file. That will make my 
pages smaller, but I can't use the velocity template engine inside .js 
files. Is there some way to not lose this functionality when working 
with external js files? How do you deal with this?

Thanks.

-- 
Bernardo Arlandis Mañó
* http://berarma.webhop.org/
* http://mozilla.webhop.org/


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Generating javascript code with velocity

Posted by Bernardo Arlandis Mañó <be...@ya.com>.
Jeffrey D. Brekke wrote:

>>>>>>On Fri, 28 May 2004 14:09:59 +0200, Bernardo Arlandis Mañó <be...@ya.com> said:
>>>>>>            
>>>>>>
>
>[SNIPPED]
>
>  
>
>>You've pointed something important, if the js code has to be
>>regenerated there's no improvement other than having separate
>>files. The js code should be static, but then I'd need to pass
>>dinamically created collections of objects to the javascript
>>functions to do their work. What I'd like to know is easy ways to do
>>this or alternative ones with the same effect. For example, I need
>>to pass a list of java objects generated by velocity to the
>>javascript functions so that the js functions can work with them
>>without having to use velocity to replicate the js code.
>>    
>>
>
>We've done this.  We put our javascript code into .js files as
>suggested.  This is static js code, functions really.  Then in our
>velocity templates we use the js functions.  We really try to keep
>these javascript functions to ui issues ( speed ) and very little
>logic in them ( for example disabling or enabling a button on the fly
>).
>
>So in one instance we had to take a list of objects we get from our
>pull tool and slam that into a js array for our javascript functions
>to use.  In our velocity template we did just that:
>
>--- CommonJS.vm--- 
>
><script language="JavaScript" src="$content.getURI("javascript/selectbox.js")"></script>
><script language="JavaScript" src="$content.getURI("javascript/format-utility.js")"></script>
>
>#set($jsfields = $layoutholder.Layout.Fields)
>#if ($jsfields)
>
><script language="JavaScript">
>
>var noFields = $jsfields.size();
>var fields = new Array(noFields);
>
>#set($offset = 1)
>#foreach($field in $jsfields)
>   #set ($i = $velocityCount - 1)
>   #set ($end = $offset + $field.FieldLength - 1 )
>   fields[$i] = new Array(6);
>   fields[$i][0] = "$formatutil.escapeJavaScript($!field.CustomerField)";
>   fields[$i][1] = "$!field.QdsField";
>   fields[$i][2] = "$!field.FieldLength";
>   fields[$i][3] = "$offset";
>   fields[$i][4] = "$end";
>   fields[$i][5] = "$!field.FieldType";
>   fields[$i][6] = "$!field.FieldFormat";
>   #set ($offset = $end+1);
>#end
></script>
>#end
>
>Then we use our javascript function on our html elements, passing the array as needed.
>
><select name="fmt" size="10" onChange="update(this, fields)" onkeypress="keyUpdate(this,event)">
>
>Hope this helps.
>
>  
>
I was going to do something like that but I was looking for a more 
general solution that could work the same for any object without much 
coding. As it seems there's no such method, I'll take yours with your 
code as a working example. Thanks.

Regards.

-- 
Bernardo Arlandis Mañó
* http://berarma.webhop.org/
* http://mozilla.webhop.org/


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Generating javascript code with velocity

Posted by "Jeffrey D. Brekke" <jb...@wi.rr.com>.
>>>>> On Fri, 28 May 2004 14:09:59 +0200, Bernardo Arlandis Mañó <be...@ya.com> said:

[SNIPPED]

> You've pointed something important, if the js code has to be
> regenerated there's no improvement other than having separate
> files. The js code should be static, but then I'd need to pass
> dinamically created collections of objects to the javascript
> functions to do their work. What I'd like to know is easy ways to do
> this or alternative ones with the same effect. For example, I need
> to pass a list of java objects generated by velocity to the
> javascript functions so that the js functions can work with them
> without having to use velocity to replicate the js code.

We've done this.  We put our javascript code into .js files as
suggested.  This is static js code, functions really.  Then in our
velocity templates we use the js functions.  We really try to keep
these javascript functions to ui issues ( speed ) and very little
logic in them ( for example disabling or enabling a button on the fly
).

So in one instance we had to take a list of objects we get from our
pull tool and slam that into a js array for our javascript functions
to use.  In our velocity template we did just that:

--- CommonJS.vm--- 

<script language="JavaScript" src="$content.getURI("javascript/selectbox.js")"></script>
<script language="JavaScript" src="$content.getURI("javascript/format-utility.js")"></script>

#set($jsfields = $layoutholder.Layout.Fields)
#if ($jsfields)

<script language="JavaScript">

var noFields = $jsfields.size();
var fields = new Array(noFields);

#set($offset = 1)
#foreach($field in $jsfields)
   #set ($i = $velocityCount - 1)
   #set ($end = $offset + $field.FieldLength - 1 )
   fields[$i] = new Array(6);
   fields[$i][0] = "$formatutil.escapeJavaScript($!field.CustomerField)";
   fields[$i][1] = "$!field.QdsField";
   fields[$i][2] = "$!field.FieldLength";
   fields[$i][3] = "$offset";
   fields[$i][4] = "$end";
   fields[$i][5] = "$!field.FieldType";
   fields[$i][6] = "$!field.FieldFormat";
   #set ($offset = $end+1);
#end
</script>
#end

Then we use our javascript function on our html elements, passing the array as needed.

<select name="fmt" size="10" onChange="update(this, fields)" onkeypress="keyUpdate(this,event)">

Hope this helps.

-- 
=====================================================================
Jeffrey D. Brekke                                   jbrekke@wi.rr.com
Wisconsin,  USA                                     brekke@apache.org
                                                    ekkerbj@yahoo.com


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Generating javascript code with velocity

Posted by Bernardo Arlandis Mañó <be...@ya.com>.
Jeffery Painter wrote:

>My experience has been to move as much code as possible into .js files 
>that do not require velocity processing as the .js files can be cached by 
>the browser. However, by the nature of using velocity to generate 
>javascript code, isn't it dynamic? I don't think there is an easy answer 
>to your question.
>
>Could you create a Javascript screen as we have seen others creating 
>dynamic charts and linking to them in that manner, I would think maybe you 
>can create a Javascript screen and have your code generated there as you 
>call it from within the main .vm you are currently working on.
>
>This still doesn't seem to have much benefit other than seperating the 
>location of the code from the screen.
>
>	Jeff Painter
>
>On Fri, 28 May 2004, Bernardo Arlandis Mañó wrote:
>
>  
>
>>Hi. I've been using turbine for a while and i've been using velocity for 
>>generating html and javascript code inside .vm files, but now I'd like 
>>to take the javascript code out to its own .js file. That will make my 
>>pages smaller, but I can't use the velocity template engine inside .js 
>>files. Is there some way to not lose this functionality when working 
>>with external js files? How do you deal with this?
>>
>>Thanks.
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: turbine-user-help@jakarta.apache.org
>
>
>
>  
>
You've pointed something important, if the js code has to be regenerated 
there's no improvement other than having separate files. The js code 
should be static, but then I'd need to pass dinamically created 
collections of objects to the javascript functions to do their work. 
What I'd like to know is easy ways to do this or alternative ones with 
the same effect. For example, I need to pass a list of java objects 
generated by velocity to the javascript functions so that the js 
functions can work with them without having to use velocity to replicate 
the js code.

Thanks for your ideas.

-- 
Bernardo Arlandis Mañó
* http://berarma.webhop.org/
* http://mozilla.webhop.org/


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Generating javascript code with velocity

Posted by Jeffery Painter <pa...@kiasoft.com>.
My experience has been to move as much code as possible into .js files 
that do not require velocity processing as the .js files can be cached by 
the browser. However, by the nature of using velocity to generate 
javascript code, isn't it dynamic? I don't think there is an easy answer 
to your question.

Could you create a Javascript screen as we have seen others creating 
dynamic charts and linking to them in that manner, I would think maybe you 
can create a Javascript screen and have your code generated there as you 
call it from within the main .vm you are currently working on.

This still doesn't seem to have much benefit other than seperating the 
location of the code from the screen.

	Jeff Painter

On Fri, 28 May 2004, Bernardo Arlandis Mañó wrote:

> Hi. I've been using turbine for a while and i've been using velocity for 
> generating html and javascript code inside .vm files, but now I'd like 
> to take the javascript code out to its own .js file. That will make my 
> pages smaller, but I can't use the velocity template engine inside .js 
> files. Is there some way to not lose this functionality when working 
> with external js files? How do you deal with this?
> 
> Thanks.
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org