psycho.velocity
psycho.velocity

Reputation: 45

Javascript code to apache velocity

I'm a beginner at velocity but I really want to learn. I did a page with various products like in an e-commerce site.

I wanted to add the selected products via check boxes to an array and did this javascript code.

var test = [];

$(":checkbox").change(function(){
  if((this).checked){
    test.push(this.id);
  }
  else{
    test.splice(test.indexOf(this.id), 1);
  }    
});

now I want to try to convert this code so that it can be used in velocity. So far I can only set the array in velocity like this:

#set ($test = [] )

and I have a vague idea that you can add items to the array in velocity like this:

#set ( $sample = $test.add("sample") )

but I have no idea how to do the checkbox thing

is there a way to do this in velocity? or at least a way to use this code.

Upvotes: 2

Views: 807

Answers (1)

MyBrainHurts
MyBrainHurts

Reputation: 2630

You can't transfer the JavaScript code to Velocity completely. At least not if you expect the same behavior of your page.

That's because your Velocity template is rendered server sided and you'll get nothing but HTML (including scripts of course). Meaning, when you see your page there is no Velocity functionality left that could react on your click on the checkbox.

So, if you want to stick to Velocity you can evaluate the checkbox in a further request after submitting the page with common methods and change whatever backing object there is.

If you need this page to react immediately on a click, this has to done client sided, hence you must leave the JavaScript code in the page. Here you can decide whether to let JavaScript do all the work or to make an asynchronous request and reload just parts of the page.

Upvotes: 1

Related Questions