You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Julien Martin <ba...@gmail.com> on 2012/01/28 20:43:17 UTC

Referencing the context path from within Javascript?

Hello,

I would like to avoid hard-coding the context path for the *url* variable
below:

* <script type="text/javascript">*
* jQuery(document).ready(function() {*
* jQuery("#accountPostcode").autocomplete({*
* source : function(request, response) {*
* var jqxhr = jQuery.ajax({*
* url : "
http://localhost:8080/bignibou-web/utils/JSonPostcodesWithQueryParam",*
* type: 'GET',*
* data: { term: request.term },*
* dataType : "json"*
* }).fail(function() { *
* console.log("error:");*
* console.log(jqxhr.statusText);*
* }).success(function(data) {*
*            response(data);*
*        });*
* },*
* minLength : 2*
* });*
* });*
* </script>*

How can I achieve this with T5?

Thanks in advance,

Julien.

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sat, 28 Jan 2012 20:12:06 -0200, Julien Martin <ba...@gmail.com> wrote:

>> Hello Thiago,

Hi!

> It is on a page template. The advice you provide does work. However, I  
> am realizing I need to concatenate the protocol+host+etc. which is a bit
> cumbersome.

Page templates don't handle anything. Pages, components (through events  
URLs), request filters (not common) and dispatchers (common). I still  
don't know what you're doing unless you post the code.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
2012/1/28 Julien Martin <ba...@gmail.com>

> Hello Thiago,
>
> It is on a page template. The advice you provide does work. However, I am
> realizing I need to concatenate the protocol+host+etc. which is a bit
> cumbersome.
>
> The problem I have is that I don't understand how the path is worked out.
> My javascript is located at the following path:
> "/context-path/account/childminderRegistration" and I need to resolve the
> following path: "/context-path/utils/JSonPostcodesWithQueryParam" for the
> url javascript variable.
>
> Is there a simpler way for my script to hit the proper path?
>
> Regards,
> J.
>
> 2012/1/28 Thiago H. de Paula Figueiredo <th...@gmail.com>
>
>> On Sat, 28 Jan 2012 17:43:17 -0200, Julien Martin <ba...@gmail.com>
>> wrote:
>>
>>  Hello,
>>>
>>
>> Hi!
>>
>>
>>  How can I achieve this with T5?
>>>
>>
>> As a general answer, you can @Inject HttpServletRequest and use its
>> getContextPath() method. Depending on who handles this URL (a Tapestry
>> page? a Tapestry page event handler method), the response would be
>> different.
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> and instructor
>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> http://www.arsmachina.com.br
>>
>
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 30 Jan 2012 11:03:16 -0200, Julien Martin <ba...@gmail.com> wrote:

> @Thiago,
> I actually wanted to avoid an additional dependency.

That's ok, but then you're not using a stack. It would have worked if you  
included it as a library. I've never used tapestry5-jquery myself, but I  
do think it deserves to be tried at least once. ;)

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
@Thiago,
I actually wanted to avoid an additional dependency.
I am going to include the autocomplete javascript within the root layout as
a library.

@All
Thanks all for helping me with this issue!! I understand how Tapestry deals
with javascript much better now!! [?]

Regards,
Julien.

2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>

> On Mon, 30 Jan 2012 10:49:23 -0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  I tried using the "@Import stack=" in order to include the jquery
>> libraries to no avail.
>> I get this:
>> Render queue error in SetupRender[Index:**childminderlayout.layout]: No
>> JavaScriptStack with name 'context:/js/jquery-1.6.2.js'.
>>
>
> Are you using tapestry5-jquery? It should take care of it. In addition, to
> use 'context:...', you should use @Import(library="..."), not stack.
>
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 30 Jan 2012 10:49:23 -0200, Julien Martin <ba...@gmail.com> wrote:

> I tried using the "@Import stack=" in order to include the jquery  
> libraries to no avail.
> I get this:
> Render queue error in SetupRender[Index:childminderlayout.layout]: No
> JavaScriptStack with name 'context:/js/jquery-1.6.2.js'.

Are you using tapestry5-jquery? It should take care of it. In addition, to  
use 'context:...', you should use @Import(library="..."), not stack.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
I tried using the "@Import stack=" in order to include the jquery libraries
to no avail.
I get this:
Render queue error in SetupRender[Index:childminderlayout.layout]: No
JavaScriptStack with name 'context:/js/jquery-1.6.2.js'.
Any one has any clue?
Regards,
Julien.

2012/1/30 Julien Martin <ba...@gmail.com>

> @Thiago: ok will do.
>
> 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
>
> On Mon, 30 Jan 2012 08:14:40 -0200, Julien Martin <ba...@gmail.com>
>> wrote:
>>
>>  I have actually. The issue is how to replace a variable within the js
>>> file with the appropriate value.
>>>
>>
>> You don't. Make it a parameter of a function you'll call through
>> JavaScriptSupport.addScript().
>>
>> To include a JS file, use @Import. addScript() is used to add lines of
>> JavaScript at the bottom of your page, typically to do initialization
>> stuff. JumpStart has one nice example: http://jumpstart.**
>> doublenegative.com.au/**jumpstart/examples/javascript/**reusable<http://jumpstart.doublenegative.com.au/jumpstart/examples/javascript/reusable>
>> .
>>
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> and instructor
>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> http://www.arsmachina.com.br
>>
>
>

Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
@Thiago: ok will do.

2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>

> On Mon, 30 Jan 2012 08:14:40 -0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  I have actually. The issue is how to replace a variable within the js
>> file with the appropriate value.
>>
>
> You don't. Make it a parameter of a function you'll call through
> JavaScriptSupport.addScript().
>
> To include a JS file, use @Import. addScript() is used to add lines of
> JavaScript at the bottom of your page, typically to do initialization
> stuff. JumpStart has one nice example: http://jumpstart.**
> doublenegative.com.au/**jumpstart/examples/javascript/**reusable<http://jumpstart.doublenegative.com.au/jumpstart/examples/javascript/reusable>
> .
>
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 30 Jan 2012 08:14:40 -0200, Julien Martin <ba...@gmail.com> wrote:

> I have actually. The issue is how to replace a variable within the js  
> file with the appropriate value.

You don't. Make it a parameter of a function you'll call through  
JavaScriptSupport.addScript().

To include a JS file, use @Import. addScript() is used to add lines of  
JavaScript at the bottom of your page, typically to do initialization  
stuff. JumpStart has one nice example:  
http://jumpstart.doublenegative.com.au/jumpstart/examples/javascript/reusable.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
You're right François,
It is an issue with the inclusion order of my files.
I noticed that the files @Import(ed) by my main layout component come after
the files @Import(ed) by the pages that use that layout. It should be the
other way around.
Is there a way to have more control over that?
Julien.


Le 30 janvier 2012 13:06, François Facon <fr...@atos.net> a écrit :

> This should work. Perhaps a conflict regading inclusion order of your js
> file.
> At this point you have to debug with firebug to see why
> Tapestry.Initializer.bignibou_autocomplete()  is not defined.
> perhaps extend failed as jquery was  already there.
>
> Regards
> François
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Referencing the context path from within Javascript?

Posted by François Facon <fr...@atos.net>.
This should work. Perhaps a conflict regading inclusion order of your js file.
At this point you have to debug with firebug to see why
Tapestry.Initializer.bignibou_autocomplete()  is not defined.
perhaps extend failed as jquery was  already there.

Regards
François

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Hi François,
I like your solution as it seems more flexible. I tried to implement it as
follows:

/js/jquery.autocomplete.js:
*jQuery.noConfict();*
*(function($) {*
* $.extend(Tapestry.Initializer, {*
* bignibou_autocomplete : function(specs) {*
* $("#accountPostcode").autocomplete({*
* source : function(request, response) {*
* var jqxhr = $.ajax({*
* url : specs.link,*
* type : "GET",*
* data : {*
* term : request.term*
* },*
* dataType : "json"*
* }).fail(function() {*
* console.log("error:");*
* console.log(jqxhr.statusText);*
* }).success(function(data) {*
* response(data);*
* });*
* },*
* minLength : 2*
* });*
* }*
* });*
*})(jQuery);*
*
*
>From my T5 java page:
*
*
*@Import(library = "context:/js/jquery.autocomplete.js")*
*...*
*
@AfterRender
public void afterRender() {
Link jSonPostcodesWithQueryParamLink =
pageRenderLinkSource.createPageRenderLink(JSonPostcodesWithQueryParam.class);
String link = jSonPostcodesWithQueryParamLink.toAbsoluteURI();

JSONObject specs = new JSONObject();
specs.put("link", link);

javaScriptSupport.addInitializerCall("bignibou_autocomplete", specs);
}
*

I get the following T5 error:
Function Tapestry.Initializer.bignibou_autocomplete() does not exist.

Any idea what I am getting wrong?
Regards,
J.
2012/1/30 Julien Martin <ba...@gmail.com>

> Thanks Lenny,
> Is the dependency to "com.flowlogix.web.services.AssetMinimizer"
> necessary?
> Is there a "pure-T5" way of retrieving the string from the script?
> Here is what I came up with borrowing from your code:
>
>
> *javaScriptSupport.addScript(getString(openStream(jsAutocomplete)), link);
> *
>
> *
> *
> *
> private String getString(InputStream is) throws IOException {
>  BufferedReader br = new BufferedReader(new InputStreamReader(is));
> StringBuilder sb = new StringBuilder();
>  String line = null;
>
> while ((line = br.readLine()) != null) {
>  sb.append(line).append("\n");
> }
>
> return sb.toString();
> }
>
> private InputStream openStream(Asset asset) throws IOException {
>  return asset.getResource().openStream();
> }
> *
>
> It is very low-level but does work.
> regards,
> J.
>
> 2012/1/30 Lenny Primak <lp...@hope.nyc.ny.us>
>
>> Take a look at these:
>>
>>
>> http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/java/com/flowlogix/web/mixins/ColorHighlight.java
>>
>> http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/resources/com/flowlogix/web/mixins/ColorHighlight.js
>>
>> On Jan 30, 2012, at 5:14 AM, Julien Martin wrote:
>>
>> > Thanks François,
>> > I have actually. The issue is how to replace a variable within the js
>> file
>> > with the appropriate value. This seems easy enough with an inline
>> script as
>> > above but if the script is located in its own  file I don't know how to
>> > achieve the desired effect.
>> > Regards,
>> > Julien.
>> >
>> > Le 30 janvier 2012 11:04, François Facon <fr...@atos.net> a
>> écrit :
>> >
>> >> Hi Julien,
>> >>
>> >> did you try the use of @Import to import your js file as explained
>> >> http://tapestry.apache.org/javascript.html?
>> >>
>> >> Regards
>> >> François
>> >>
>> >> 2012/1/30 Julien Martin <ba...@gmail.com>:
>> >>> Hi Thiago!
>> >>>
>> >>> Using your advice, I managed to generate the link easily. I have also
>> >> moved
>> >>> the script to a separate file as advised.
>> >>>
>> >>> Now what remains unclear to me is that the *addScript* method takes a
>> >>> String and not a url to my javascript file.
>> >>>
>> >>> So on the one hand I have my link, on the other my javascript file. I
>> >>> understand the rationale behind the addScript method: formatting the
>> >>> content of the js file with one or several variables passed as the
>> second
>> >>> argument but what I have is js file not a short script as in the
>> >>> documentation:
>> >>>
>> >>> javaScriptSupport.addScript(
>> >>>
>> >>>       "$('%s').observe('click', hideMe());",
>> >>>       container.getClientId());
>> >>>
>> >>> How do I pass my js file to the addScript method or do I use another
>> >> method?
>> >>>
>> >>> Regards,
>> >>>
>> >>> Julien.
>> >>>
>> >>> 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
>> >>>
>> >>>> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
>> >>>> wrote:
>> >>>>
>> >>>> Thiago,
>> >>>>>
>> >>>>
>> >>>> Hi!
>> >>>>
>> >>>>
>> >>>> Yes it is indeed a Tapestry page. Here is the java code for it (there
>> >> is
>> >>>>> no template for this page):
>> >>>>>
>> >>>>
>> >>>> So the correct way of generating an URL for a page is to @Inject
>> >>>> PageRenderLinkSource and use one of its methods. They return a Link.
>> >> Pass
>> >>>> the result of the toAbsoluteURI() method to your JavaScript method by
>> >>>> @Inject'ing JavaScriptSupport and using its addScript() method.
>> >>>>
>> >>>> In addition, as you're returning JSON content, instead of return
>> >>>> TextStreamResponse, you can return a JSONObject or a JSONArray.
>> >>>>
>> >>>>
>> >>>> --
>> >>>> Thiago H. de Paula Figueiredo
>> >>>> Independent Java, Apache Tapestry 5 and Hibernate consultant,
>> developer,
>> >>>> and instructor
>> >>>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> >>>> http://www.arsmachina.com.br
>> >>>>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> >> For additional commands, e-mail: users-help@tapestry.apache.org
>> >>
>> >>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>

Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Thanks Lenny,
Is the dependency to "com.flowlogix.web.services.AssetMinimizer" necessary?
Is there a "pure-T5" way of retrieving the string from the script?
Here is what I came up with borrowing from your code:


*javaScriptSupport.addScript(getString(openStream(jsAutocomplete)), link);*

*
*
*
private String getString(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}

return sb.toString();
}

private InputStream openStream(Asset asset) throws IOException {
return asset.getResource().openStream();
}
*

It is very low-level but does work.
regards,
J.

2012/1/30 Lenny Primak <lp...@hope.nyc.ny.us>

> Take a look at these:
>
>
> http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/java/com/flowlogix/web/mixins/ColorHighlight.java
>
> http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/resources/com/flowlogix/web/mixins/ColorHighlight.js
>
> On Jan 30, 2012, at 5:14 AM, Julien Martin wrote:
>
> > Thanks François,
> > I have actually. The issue is how to replace a variable within the js
> file
> > with the appropriate value. This seems easy enough with an inline script
> as
> > above but if the script is located in its own  file I don't know how to
> > achieve the desired effect.
> > Regards,
> > Julien.
> >
> > Le 30 janvier 2012 11:04, François Facon <fr...@atos.net> a
> écrit :
> >
> >> Hi Julien,
> >>
> >> did you try the use of @Import to import your js file as explained
> >> http://tapestry.apache.org/javascript.html?
> >>
> >> Regards
> >> François
> >>
> >> 2012/1/30 Julien Martin <ba...@gmail.com>:
> >>> Hi Thiago!
> >>>
> >>> Using your advice, I managed to generate the link easily. I have also
> >> moved
> >>> the script to a separate file as advised.
> >>>
> >>> Now what remains unclear to me is that the *addScript* method takes a
> >>> String and not a url to my javascript file.
> >>>
> >>> So on the one hand I have my link, on the other my javascript file. I
> >>> understand the rationale behind the addScript method: formatting the
> >>> content of the js file with one or several variables passed as the
> second
> >>> argument but what I have is js file not a short script as in the
> >>> documentation:
> >>>
> >>> javaScriptSupport.addScript(
> >>>
> >>>       "$('%s').observe('click', hideMe());",
> >>>       container.getClientId());
> >>>
> >>> How do I pass my js file to the addScript method or do I use another
> >> method?
> >>>
> >>> Regards,
> >>>
> >>> Julien.
> >>>
> >>> 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
> >>>
> >>>> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
> >>>> wrote:
> >>>>
> >>>> Thiago,
> >>>>>
> >>>>
> >>>> Hi!
> >>>>
> >>>>
> >>>> Yes it is indeed a Tapestry page. Here is the java code for it (there
> >> is
> >>>>> no template for this page):
> >>>>>
> >>>>
> >>>> So the correct way of generating an URL for a page is to @Inject
> >>>> PageRenderLinkSource and use one of its methods. They return a Link.
> >> Pass
> >>>> the result of the toAbsoluteURI() method to your JavaScript method by
> >>>> @Inject'ing JavaScriptSupport and using its addScript() method.
> >>>>
> >>>> In addition, as you're returning JSON content, instead of return
> >>>> TextStreamResponse, you can return a JSONObject or a JSONArray.
> >>>>
> >>>>
> >>>> --
> >>>> Thiago H. de Paula Figueiredo
> >>>> Independent Java, Apache Tapestry 5 and Hibernate consultant,
> developer,
> >>>> and instructor
> >>>> Owner, Ars Machina Tecnologia da Informação Ltda.
> >>>> http://www.arsmachina.com.br
> >>>>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Referencing the context path from within Javascript?

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
Take a look at these:

http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/java/com/flowlogix/web/mixins/ColorHighlight.java
http://code.google.com/p/flowlogix/source/browse/tapestry-services/src/main/resources/com/flowlogix/web/mixins/ColorHighlight.js

On Jan 30, 2012, at 5:14 AM, Julien Martin wrote:

> Thanks François,
> I have actually. The issue is how to replace a variable within the js file
> with the appropriate value. This seems easy enough with an inline script as
> above but if the script is located in its own  file I don't know how to
> achieve the desired effect.
> Regards,
> Julien.
> 
> Le 30 janvier 2012 11:04, François Facon <fr...@atos.net> a écrit :
> 
>> Hi Julien,
>> 
>> did you try the use of @Import to import your js file as explained
>> http://tapestry.apache.org/javascript.html?
>> 
>> Regards
>> François
>> 
>> 2012/1/30 Julien Martin <ba...@gmail.com>:
>>> Hi Thiago!
>>> 
>>> Using your advice, I managed to generate the link easily. I have also
>> moved
>>> the script to a separate file as advised.
>>> 
>>> Now what remains unclear to me is that the *addScript* method takes a
>>> String and not a url to my javascript file.
>>> 
>>> So on the one hand I have my link, on the other my javascript file. I
>>> understand the rationale behind the addScript method: formatting the
>>> content of the js file with one or several variables passed as the second
>>> argument but what I have is js file not a short script as in the
>>> documentation:
>>> 
>>> javaScriptSupport.addScript(
>>> 
>>>       "$('%s').observe('click', hideMe());",
>>>       container.getClientId());
>>> 
>>> How do I pass my js file to the addScript method or do I use another
>> method?
>>> 
>>> Regards,
>>> 
>>> Julien.
>>> 
>>> 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
>>> 
>>>> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
>>>> wrote:
>>>> 
>>>> Thiago,
>>>>> 
>>>> 
>>>> Hi!
>>>> 
>>>> 
>>>> Yes it is indeed a Tapestry page. Here is the java code for it (there
>> is
>>>>> no template for this page):
>>>>> 
>>>> 
>>>> So the correct way of generating an URL for a page is to @Inject
>>>> PageRenderLinkSource and use one of its methods. They return a Link.
>> Pass
>>>> the result of the toAbsoluteURI() method to your JavaScript method by
>>>> @Inject'ing JavaScriptSupport and using its addScript() method.
>>>> 
>>>> In addition, as you're returning JSON content, instead of return
>>>> TextStreamResponse, you can return a JSONObject or a JSONArray.
>>>> 
>>>> 
>>>> --
>>>> Thiago H. de Paula Figueiredo
>>>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>>>> and instructor
>>>> Owner, Ars Machina Tecnologia da Informação Ltda.
>>>> http://www.arsmachina.com.br
>>>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by François Facon <fr...@atos.net>.
It would be better to use js object and pass parameter to constructor.

for prototype see
http://wiki.apache.org/tapestry/Tapestry5AndJavaScriptExplained
http://jumpstart.doublenegative.com.au/jumpstart/examples/javascript/robust

for jquery
https://github.com/got5/tapestry5-jquery/blob/master/src/main/java/org/got5/tapestry5/jquery/components/InPlaceEditor.java

void afterRender(MarkupWriter writer)
	{
		Link link = resources.createEventLink(EventConstants.ACTION, contextArray);
		JSONObject spec = new JSONObject();

		writer.end();

		spec.put("clientId", getClientId());
		spec.put("href", link.toURI());


and related js

https://github.com/got5/tapestry5-jquery/blob/master/src/main/resources/org/got5/tapestry5/jquery/assets/components/jeditable/jeditable.js

Hope this help

Best
François

2012/1/30 Julien Martin <ba...@gmail.com>:
> Thanks François,
> I have actually. The issue is how to replace a variable within the js file
> with the appropriate value. This seems easy enough with an inline script as
> above but if the script is located in its own  file I don't know how to
> achieve the desired effect.
> Regards,
> Julien.
>
> Le 30 janvier 2012 11:04, François Facon <fr...@atos.net> a écrit :
>
>> Hi Julien,
>>
>> did you try the use of @Import to import your js file as explained
>> http://tapestry.apache.org/javascript.html?
>>
>> Regards
>> François
>>
>> 2012/1/30 Julien Martin <ba...@gmail.com>:
>> > Hi Thiago!
>> >
>> > Using your advice, I managed to generate the link easily. I have also
>> moved
>> > the script to a separate file as advised.
>> >
>> > Now what remains unclear to me is that the *addScript* method takes a
>> > String and not a url to my javascript file.
>> >
>> > So on the one hand I have my link, on the other my javascript file. I
>> > understand the rationale behind the addScript method: formatting the
>> > content of the js file with one or several variables passed as the second
>> > argument but what I have is js file not a short script as in the
>> > documentation:
>> >
>> > javaScriptSupport.addScript(
>> >
>> >        "$('%s').observe('click', hideMe());",
>> >        container.getClientId());
>> >
>> > How do I pass my js file to the addScript method or do I use another
>> method?
>> >
>> > Regards,
>> >
>> > Julien.
>> >
>> > 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
>> >
>> >> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
>> >> wrote:
>> >>
>> >>  Thiago,
>> >>>
>> >>
>> >> Hi!
>> >>
>> >>
>> >>  Yes it is indeed a Tapestry page. Here is the java code for it (there
>> is
>> >>> no template for this page):
>> >>>
>> >>
>> >> So the correct way of generating an URL for a page is to @Inject
>> >> PageRenderLinkSource and use one of its methods. They return a Link.
>> Pass
>> >> the result of the toAbsoluteURI() method to your JavaScript method by
>> >> @Inject'ing JavaScriptSupport and using its addScript() method.
>> >>
>> >> In addition, as you're returning JSON content, instead of return
>> >> TextStreamResponse, you can return a JSONObject or a JSONArray.
>> >>
>> >>
>> >> --
>> >> Thiago H. de Paula Figueiredo
>> >> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> >> and instructor
>> >> Owner, Ars Machina Tecnologia da Informação Ltda.
>> >> http://www.arsmachina.com.br
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Thanks François,
I have actually. The issue is how to replace a variable within the js file
with the appropriate value. This seems easy enough with an inline script as
above but if the script is located in its own  file I don't know how to
achieve the desired effect.
Regards,
Julien.

Le 30 janvier 2012 11:04, François Facon <fr...@atos.net> a écrit :

> Hi Julien,
>
> did you try the use of @Import to import your js file as explained
> http://tapestry.apache.org/javascript.html?
>
> Regards
> François
>
> 2012/1/30 Julien Martin <ba...@gmail.com>:
> > Hi Thiago!
> >
> > Using your advice, I managed to generate the link easily. I have also
> moved
> > the script to a separate file as advised.
> >
> > Now what remains unclear to me is that the *addScript* method takes a
> > String and not a url to my javascript file.
> >
> > So on the one hand I have my link, on the other my javascript file. I
> > understand the rationale behind the addScript method: formatting the
> > content of the js file with one or several variables passed as the second
> > argument but what I have is js file not a short script as in the
> > documentation:
> >
> > javaScriptSupport.addScript(
> >
> >        "$('%s').observe('click', hideMe());",
> >        container.getClientId());
> >
> > How do I pass my js file to the addScript method or do I use another
> method?
> >
> > Regards,
> >
> > Julien.
> >
> > 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
> >
> >> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
> >> wrote:
> >>
> >>  Thiago,
> >>>
> >>
> >> Hi!
> >>
> >>
> >>  Yes it is indeed a Tapestry page. Here is the java code for it (there
> is
> >>> no template for this page):
> >>>
> >>
> >> So the correct way of generating an URL for a page is to @Inject
> >> PageRenderLinkSource and use one of its methods. They return a Link.
> Pass
> >> the result of the toAbsoluteURI() method to your JavaScript method by
> >> @Inject'ing JavaScriptSupport and using its addScript() method.
> >>
> >> In addition, as you're returning JSON content, instead of return
> >> TextStreamResponse, you can return a JSONObject or a JSONArray.
> >>
> >>
> >> --
> >> Thiago H. de Paula Figueiredo
> >> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> >> and instructor
> >> Owner, Ars Machina Tecnologia da Informação Ltda.
> >> http://www.arsmachina.com.br
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Referencing the context path from within Javascript?

Posted by François Facon <fr...@atos.net>.
Hi Julien,

did you try the use of @Import to import your js file as explained
http://tapestry.apache.org/javascript.html?

Regards
François

2012/1/30 Julien Martin <ba...@gmail.com>:
> Hi Thiago!
>
> Using your advice, I managed to generate the link easily. I have also moved
> the script to a separate file as advised.
>
> Now what remains unclear to me is that the *addScript* method takes a
> String and not a url to my javascript file.
>
> So on the one hand I have my link, on the other my javascript file. I
> understand the rationale behind the addScript method: formatting the
> content of the js file with one or several variables passed as the second
> argument but what I have is js file not a short script as in the
> documentation:
>
> javaScriptSupport.addScript(
>
>        "$('%s').observe('click', hideMe());",
>        container.getClientId());
>
> How do I pass my js file to the addScript method or do I use another method?
>
> Regards,
>
> Julien.
>
> 2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>
>
>> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
>> wrote:
>>
>>  Thiago,
>>>
>>
>> Hi!
>>
>>
>>  Yes it is indeed a Tapestry page. Here is the java code for it (there is
>>> no template for this page):
>>>
>>
>> So the correct way of generating an URL for a page is to @Inject
>> PageRenderLinkSource and use one of its methods. They return a Link. Pass
>> the result of the toAbsoluteURI() method to your JavaScript method by
>> @Inject'ing JavaScriptSupport and using its addScript() method.
>>
>> In addition, as you're returning JSON content, instead of return
>> TextStreamResponse, you can return a JSONObject or a JSONArray.
>>
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> and instructor
>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> http://www.arsmachina.com.br
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Hi Thiago!

Using your advice, I managed to generate the link easily. I have also moved
the script to a separate file as advised.

Now what remains unclear to me is that the *addScript* method takes a
String and not a url to my javascript file.

So on the one hand I have my link, on the other my javascript file. I
understand the rationale behind the addScript method: formatting the
content of the js file with one or several variables passed as the second
argument but what I have is js file not a short script as in the
documentation:

javaScriptSupport.addScript(

        "$('%s').observe('click', hideMe());",
        container.getClientId());

How do I pass my js file to the addScript method or do I use another method?

Regards,

Julien.

2012/1/30 Thiago H. de Paula Figueiredo <th...@gmail.com>

> On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  Thiago,
>>
>
> Hi!
>
>
>  Yes it is indeed a Tapestry page. Here is the java code for it (there is
>> no template for this page):
>>
>
> So the correct way of generating an URL for a page is to @Inject
> PageRenderLinkSource and use one of its methods. They return a Link. Pass
> the result of the toAbsoluteURI() method to your JavaScript method by
> @Inject'ing JavaScriptSupport and using its addScript() method.
>
> In addition, as you're returning JSON content, instead of return
> TextStreamResponse, you can return a JSONObject or a JSONArray.
>
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, 29 Jan 2012 19:22:31 -0200, Julien Martin <ba...@gmail.com> wrote:

> Thiago,

Hi!

> Yes it is indeed a Tapestry page. Here is the java code for it (there is  
> no template for this page):

So the correct way of generating an URL for a page is to @Inject  
PageRenderLinkSource and use one of its methods. They return a Link. Pass  
the result of the toAbsoluteURI() method to your JavaScript method by  
@Inject'ing JavaScriptSupport and using its addScript() method.

In addition, as you're returning JSON content, instead of return  
TextStreamResponse, you can return a JSONObject or a JSONArray.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Thiago,

Yes it is indeed a Tapestry page. Here is the java code for it (there is no
template for this page):

*package com.bignibou.web.pages.utils;*
*
*
*import java.util.List;*
*
*
*import org.apache.tapestry5.EventConstants;*
*import org.apache.tapestry5.StreamResponse;*
*import org.apache.tapestry5.annotations.OnEvent;*
*import org.apache.tapestry5.annotations.RequestParameter;*
*import org.apache.tapestry5.ioc.annotations.Inject;*
*import org.apache.tapestry5.util.TextStreamResponse;*
*
*
*import com.bignibou.domain.utils.PostcodeJson;*
*import com.bignibou.service.AccountService;*
*import com.google.gson.Gson;*
*
*
*public class JSonPostcodesWithQueryParam {*
*
*
* @Inject*
* private AccountService service;*
*
*
* @OnEvent(EventConstants.ACTIVATE)*
* StreamResponse loadPostcodes(@RequestParameter(value = "term") String
beginningOfPostcode) {*
* Gson gson = new Gson();*
* List<PostcodeJson> postcodes = service.loadPostcodes(beginningOfPostcode);
*
* return new TextStreamResponse("application/json","UTF-8",
gson.toJson(postcodes));*
* }*
*}*

Regards,
Julien.

2012/1/29 Thiago H. de Paula Figueiredo <th...@gmail.com>

> On Sun, 29 Jan 2012 18:00:17 -0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  Hello Thiago,
>>
>
> Hi!
>
>
>> Here is the code from the T5 template:
>>
>> <script type="text/javascript">
>> jQuery(document).ready(**function() {
>> jQuery("#accountPostcode").**autocomplete({
>> source : function(request, response) {
>> var jqxhr = jQuery.ajax({
>> *url : "http://localhost:8080/**bignibou-web/utils/**
>> JSonPostcodesWithQueryParam<http://localhost:8080/bignibou-web/utils/JSonPostcodesWithQueryParam>
>>
>
> Is JsonPostcodesWithQueryParam a Tapestry page? By the away, avoid using
> inline JavaScript (just as you've done in the template you've posted) and
> move it to a .js file which you can include automatically using @Import.
>
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, 29 Jan 2012 18:00:17 -0200, Julien Martin <ba...@gmail.com> wrote:

> Hello Thiago,

Hi!

>
> Here is the code from the T5 template:
>
> <script type="text/javascript">
> jQuery(document).ready(function() {
> jQuery("#accountPostcode").autocomplete({
> source : function(request, response) {
> var jqxhr = jQuery.ajax({
> *url :  
> "http://localhost:8080/bignibou-web/utils/JSonPostcodesWithQueryParam

Is JsonPostcodesWithQueryParam a Tapestry page? By the away, avoid using  
inline JavaScript (just as you've done in the template you've posted) and  
move it to a .js file which you can include automatically using @Import.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Referencing the context path from within Javascript?

Posted by Julien Martin <ba...@gmail.com>.
Hello Thiago,

Here is the code from the T5 template:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#accountPostcode").autocomplete({
source : function(request, response) {
var jqxhr = jQuery.ajax({
*url : "http://localhost:8080/bignibou-web/utils/JSonPostcodesWithQueryParam
",*
type: "GET",
data: { term: request.term },
dataType : "json"
}).fail(function() {
console.log("error:");
console.log(jqxhr.statusText);
}).success(function(data) {
            response(data);
        });
},
minLength : 2
});
});
</script>
The bold line is the problematic one. I need to generate dynamically this
part of the url:
*
*

*"http://localhost:8080/bignibou-web/*


Regards,
Julien.

P.S. Sorry about the mail sent to your inbox directly.


2012/1/29 Thiago H. de Paula Figueiredo <th...@gmail.com>

> Please answer to the mailing list and not just to me. ;)
>
>
> On Sat, 28 Jan 2012 20:11:51 -0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  Hello Thiago,
>>
>> It is on a page template. The advice you provide does work. However, I am
>> realizing I need to concatenate the protocol+host+etc. which is a bit
>> cumbersome.
>>
>> The problem I have is that I don't understand how the path is worked out.
>> My javascript is located at the following path:
>> "/context-path/account/**childminderRegistration" and I need to resolve
>> the
>> following path: "/context-path/utils/**JSonPostcodesWithQueryParam" for
>> the
>> url javascript variable.
>>
>> Is there a simpler way for my script to hit the proper path?
>>
>> Regards,
>> J.
>>
>> 2012/1/28 Thiago H. de Paula Figueiredo <th...@gmail.com>
>>
>>  On Sat, 28 Jan 2012 17:43:17 -0200, Julien Martin <ba...@gmail.com>
>>> wrote:
>>>
>>>  Hello,
>>>
>>>>
>>>>
>>> Hi!
>>>
>>>
>>>  How can I achieve this with T5?
>>>
>>>>
>>>>
>>> As a general answer, you can @Inject HttpServletRequest and use its
>>> getContextPath() method. Depending on who handles this URL (a Tapestry
>>> page? a Tapestry page event handler method), the response would be
>>> different.
>>>
>>> --
>>> Thiago H. de Paula Figueiredo
>>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>>> and instructor
>>> Owner, Ars Machina Tecnologia da Informação Ltda.
>>> http://www.arsmachina.com.br
>>>
>>>
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> Consultor, desenvolvedor e instrutor em Java, Tapestry e Hibernate
> http://www.arsmachina.com.br
>

Re: Referencing the context path from within Javascript?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sat, 28 Jan 2012 17:43:17 -0200, Julien Martin <ba...@gmail.com> wrote:

> Hello,

Hi!

> How can I achieve this with T5?

As a general answer, you can @Inject HttpServletRequest and use its  
getContextPath() method. Depending on who handles this URL (a Tapestry  
page? a Tapestry page event handler method), the response would be  
different.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org