Hi,
You can't. JavaScript is code executed on the client side (inside the
browser on your local machine), while java components and velocity
represent code executed on the server side (on the remote server that is
hosting the web application).
One alternative/hack is that you have your JavaScript execute from a
*parseable=Yes* JavaScriptExtension [1] object and, inside your JS code,
you put a bit of velocity to produce the result you want from the server
component *before* you get the JavaScript to the client. This way, the
result is that your JS code is first *generated* on the server (by this I
mean that parts of your code are generated by the execution of the velocity
I just mentioned) and then it is served to the client (browser) with the
result of your component call already *embedded* in it.
Here is an example:
## This is in Velocity, executed on the server to produce the JavaScript.
#set ($serverSideComponentCallResults =
$services.myServiceOrComponent.getTheResults())
// This is in the resulting JavaScript:
var componentCallResults = new Array(
## This is in Velocity, executed on the server to produce the JavaScript.
Assuming the result of the component was a list/array of strings.
#foreach ($result in $serverSideComponentCallResults)
## Skip the first ',' for the fist element in the array.
#if ($foreach.count != 1)
,#end"$result"
#end
// This is JavaScript again, closing the array. After this, you can use the
componentCallResults array in your following JavaScript code.
);
alert(componentCallResults);
However, this only works if the result from your component is "cacheable"
(for the entire time between 2 page loads) and is of simple data types.
Keep in mind that the value of the JavaScript "componentCallResults"
variable is computed on the server, once per page load.
If you want a solution that allows you to randomly call components with
JavaScript while not leaving the page, you need to build a page (or a REST
resource) and call that page with AJAX.
Hope this helps,
Eduard
----------
[1]
http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutorial
On Wed, Jun 20, 2012 at 4:33 PM, liujinlong5788 <liujinlong5788(a)gmail.com>wrote;wrote:
Hello ,how can I call a component method in javascript
?
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs