calling a java component from JavaScriptExtension

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

calling a java component from JavaScriptExtension

gervwyk
Is it possible to call a java component from a JavaScriptExtension? like this:

In javascript:

require(['jquery'], function ($) {
   $('#ToJava').click(function(){
          var returnValue = $services.mycomponent.mymethod( someNumber );
          // use return value
   });
});

In java:

 public int mymethod(int num){
   return num +1;
}

I know that I can do the ajax round trip and invoke the method through velocity:
http://extensions.xwiki.org/xwiki/bin/view/Extension/AJAX+example

-- just wondering if there is a way to shortcut this process by avoiding velocity and only working in js?
or is my logic flawed?
Reply | Threaded
Open this post in threaded view
|

Re: calling a java component from JavaScriptExtension

Jean SIMARD
Dear gervwyk,

From what I understand of components and Javascript, it will not be
possible.  Components are Java code and therefore are executed on
server-side.  Velocity, is also script that is executed server-side.
And Javascript is client-side, so it doesn't have the logic for
components.  As far as I know, we don't have any possibility to execute
server-side Javascript in XWiki at the moment.  So a component doesn't
have much sense for Javascript.

However, instead of following [1], you may also create a REST component
on server-side (which will also allow AJAX).  Look [2] for documentation
about writing new REST components.  You'll loose a bit of flexibility
(because you'll have to build JAR and restart the wiki each time you
change source code) but you won't have to deal with Velocity.

Note there is also a new extension that may fill your need but I'm not
sure it's exactly what you need [3].

Hope it helps.

[1] http://extensions.xwiki.org/xwiki/bin/view/Extension/AJAX+example
[2]
http://platform.xwiki.org/xwiki/bin/view/Features/XWikiRESTfulAPI#HCustomresources
[2]
http://extensions.xwiki.org/xwiki/bin/view/Extension/Structured+data+access+JS+API

On 28/01/2016 10:30, gervwyk wrote:

> Is it possible to call a java component from a JavaScriptExtension? like
> this:
>
> In javascript:
>
> require(['jquery'], function ($) {
>    $('#ToJava').click(function(){
>  var returnValue = $services.mycomponent.mymethod( someNumber );
>           // use return value
>    });
> });
>
> In java:
>
>  public int mymethod(int num){
>    return num +1;
> }
>
> I know that I can do the ajax round trip and invoke the method through
> velocity:
> http://extensions.xwiki.org/xwiki/bin/view/Extension/AJAX+example
>
> -- just wondering if there is a way to shortcut this process by avoiding
> velocity and only working in js?
> or is my logic flawed?
>
>
>
>
> --
> View this message in context: http://xwiki.475771.n2.nabble.com/calling-a-java-component-from-JavaScriptExtension-tp7597705.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/users
>

--
Jean Simard
[hidden email]
Research engineer at XWiki SAS
http://www.xwiki.com
Committer on the XWiki.org project
http://www.xwiki.org
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: calling a java component from JavaScriptExtension

gervwyk
Thanks Jean. 

Yes, I understand. I think what would be useful, is a JS function to invoke a java method server-side, since I find myself doing this ajax round trip all the time. So what I'm proposing a JS function call, say:

JavaServer.call("mycomponent.mymethod", {JSON data input}, "(return type)' );

Which runs the ajax in JS required calling a generic xwiki page url with a velocity script maybe which invokes the java component passed with the JS parameter and return the "html/text" or "application/json" data as specified. 

Currently I have like 50 static terminal pages which is only used to pass data from the JS with ajax to a velocity script to a java component and back. Basically, the only difference in those velocity scripts is the method name. So kinda feel that it might be possible to "short-cut' this velocity page, with a standard velocity macro which invokes the passed method. I'm pretty sure this will add client side "reactive"-like functionality to xwiki as you can invoke server-side functions from JS events. In principle like in Meteor.js were you do a Method.call("methodname",function(){}), although the underlying architecture differs greatly.  
 
Keep up the good work
Gerrie van wyk


On Thu, Jan 28, 2016 at 11:56 AM, Jean SIMARD [via XWiki] <[hidden email]> wrote:
Dear gervwyk,

From what I understand of components and Javascript, it will not be
possible.  Components are Java code and therefore are executed on
server-side.  Velocity, is also script that is executed server-side.
And Javascript is client-side, so it doesn't have the logic for
components.  As far as I know, we don't have any possibility to execute
server-side Javascript in XWiki at the moment.  So a component doesn't
have much sense for Javascript.

However, instead of following [1], you may also create a REST component
on server-side (which will also allow AJAX).  Look [2] for documentation
about writing new REST components.  You'll loose a bit of flexibility
(because you'll have to build JAR and restart the wiki each time you
change source code) but you won't have to deal with Velocity.

Note there is also a new extension that may fill your need but I'm not
sure it's exactly what you need [3].

Hope it helps.

[1] http://extensions.xwiki.org/xwiki/bin/view/Extension/AJAX+example
[2]
http://platform.xwiki.org/xwiki/bin/view/Features/XWikiRESTfulAPI#HCustomresources
[2]
http://extensions.xwiki.org/xwiki/bin/view/Extension/Structured+data+access+JS+API

On 28/01/2016 10:30, gervwyk wrote:

> Is it possible to call a java component from a JavaScriptExtension? like
> this:
>
> In javascript:
>
> require(['jquery'], function ($) {
>    $('#ToJava').click(function(){
>  var returnValue = $services.mycomponent.mymethod( someNumber );
>           // use return value
>    });
> });
>
> In java:
>
>  public int mymethod(int num){
>    return num +1;
> }
>
> I know that I can do the ajax round trip and invoke the method through
> velocity:
> http://extensions.xwiki.org/xwiki/bin/view/Extension/AJAX+example
>
> -- just wondering if there is a way to shortcut this process by avoiding
> velocity and only working in js?
> or is my logic flawed?
>
>
>
>
> --
> View this message in context: http://xwiki.475771.n2.nabble.com/calling-a-java-component-from-JavaScriptExtension-tp7597705.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/users
>

--
Jean Simard
[hidden email]
Research engineer at XWiki SAS
http://www.xwiki.com
Committer on the XWiki.org project
http://www.xwiki.org
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users



If you reply to this email, your message will be added to the discussion below:
http://xwiki.475771.n2.nabble.com/calling-a-java-component-from-JavaScriptExtension-tp7597705p7597706.html
To unsubscribe from calling a java component from JavaScriptExtension, click here.
NAML