You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Inge Solvoll <in...@gmail.com> on 2010/02/05 09:55:28 UTC
Automatically include javascript companion
Hi!
I find myself repeating myself. Quite often when creating a page or a
component, I create a companion javascript file with the same name as my
component class. I got the idea today to try to create some kind of worker
or service that automatically includes a javascript that has the same name
as the component class and exists in the same folder on the classpath. After
all, this is how the tml and properties files are hooked up to component
classes, so why not do it for javascript as well?
Any pointers to get me started? :)
Re: Automatically include javascript companion
Posted by Inge Solvoll <in...@gmail.com>.
There you go, did it for fun :)
One thing remaining: how to find out if asset exists without an exception
being thrown?
public class IncludeJavascriptLibraryWithSameNameWorker extends
AbstractIncludeAssetWorker {
private final RenderSupport renderSupport;
private final AssetSource assetSource2;
public IncludeJavascriptLibraryWithSameNameWorker(AssetSource assetSource,
RenderSupport renderSupport, SymbolSource symbolSource) {
super(assetSource, symbolSource);
this.assetSource2 = assetSource;
this.renderSupport = renderSupport;
}
public void transform(ClassTransformation transformation, final
MutableComponentModel model) {
final Resource baseResource = model.getBaseResource();
String className = model.getComponentClassName();
String jsFileName = className.substring(className.lastIndexOf(".") +
1) + ".js";
try {
Asset asset = assetSource2.getAsset(baseResource, jsFileName, null);
if (asset != null) {
addOperationForAssetPaths(transformation, model, new String[] {
jsFileName });
}
}
catch (Exception e) {
// No default JS asset found for this class.
}
}
protected void handleAsset(Asset asset) {
renderSupport.addScriptLink(asset);
}
On Fri, Feb 5, 2010 at 9:04 PM, Josh Canfield <jo...@gmail.com>wrote:
> > But in the end, i realized that it's a silly idea that only leads to
> other
> > people having even more problems finding out what's going on in my code
> :)
>
> If you organize things so that your js lives right next to your tml
> then it shouldn't be so hard to handle. If you're looking at the
> folder you should see all the files nicely sorted together:
>
> MyComponent.tml
> MyComponent.js
> MyComponent.properties
>
> At times I've wished that including that script was the default
> behavior... I sometimes forget that with tapestry I can just make it
> so! If anyones written the class transformer to do this and wants to
> share I'd love to make use of it.
>
> Josh
>
> On Fri, Feb 5, 2010 at 10:44 AM, Inge Solvoll <in...@gmail.com>
> wrote:
> > Thanks!
> >
> > I looked into this and it wasn't too hard to figure out (as usual).
> >
> > But in the end, i realized that it's a silly idea that only leads to
> other
> > people having even more problems finding out what's going on in my code
> :) I
> > can afford the typing required to include the javascript companion for my
> > components :)
> >
> > On Fri, Feb 5, 2010 at 10:05 AM, cordenier christophe <
> > christophe.cordenier@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> Your worker should use AssetSource and RenderSupport to automatically
> add
> >> the js file if it exists, have a look at IncludeJavaScriptLibraryWorker
> >> source file to see an exemple of script injection.
> >> BTW in my experience, the multiplication of JS/CSS files is not really
> >> appreciated by IE, so if you decide to work this way i advise you to
> also
> >> turn on (default) combine-scripts feature.
> >>
> >> Regards,
> >> Christophe Cordenier
> >>
> >> Developer of wooki @wookicentral.com
> >>
> >> 2010/2/5 Inge Solvoll <in...@gmail.com>
> >>
> >> > Hi!
> >> >
> >> > I find myself repeating myself. Quite often when creating a page or a
> >> > component, I create a companion javascript file with the same name as
> my
> >> > component class. I got the idea today to try to create some kind of
> >> worker
> >> > or service that automatically includes a javascript that has the same
> >> name
> >> > as the component class and exists in the same folder on the classpath.
> >> > After
> >> > all, this is how the tml and properties files are hooked up to
> component
> >> > classes, so why not do it for javascript as well?
> >> >
> >> > Any pointers to get me started? :)
> >> >
> >>
> >>
> >>
> >> --
> >> Regards,
> >> Christophe Cordenier.
> >>
> >> Developer of wooki @wookicentral.com
> >>
> >
>
>
>
> --
> --
> http://www.bodylabgym.com - a private, by appointment only, one-on-one
> health and fitness facility.
> --
> http://www.ectransition.com - Quality Electronic Cigarettes at a
> reasonable price!
> --
> TheDailyTube.com. Sign up and get the best new videos on the internet
> delivered fresh to your inbox.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
Re: Automatically include javascript companion
Posted by Josh Canfield <jo...@gmail.com>.
> But in the end, i realized that it's a silly idea that only leads to other
> people having even more problems finding out what's going on in my code :)
If you organize things so that your js lives right next to your tml
then it shouldn't be so hard to handle. If you're looking at the
folder you should see all the files nicely sorted together:
MyComponent.tml
MyComponent.js
MyComponent.properties
At times I've wished that including that script was the default
behavior... I sometimes forget that with tapestry I can just make it
so! If anyones written the class transformer to do this and wants to
share I'd love to make use of it.
Josh
On Fri, Feb 5, 2010 at 10:44 AM, Inge Solvoll <in...@gmail.com> wrote:
> Thanks!
>
> I looked into this and it wasn't too hard to figure out (as usual).
>
> But in the end, i realized that it's a silly idea that only leads to other
> people having even more problems finding out what's going on in my code :) I
> can afford the typing required to include the javascript companion for my
> components :)
>
> On Fri, Feb 5, 2010 at 10:05 AM, cordenier christophe <
> christophe.cordenier@gmail.com> wrote:
>
>> Hi,
>>
>> Your worker should use AssetSource and RenderSupport to automatically add
>> the js file if it exists, have a look at IncludeJavaScriptLibraryWorker
>> source file to see an exemple of script injection.
>> BTW in my experience, the multiplication of JS/CSS files is not really
>> appreciated by IE, so if you decide to work this way i advise you to also
>> turn on (default) combine-scripts feature.
>>
>> Regards,
>> Christophe Cordenier
>>
>> Developer of wooki @wookicentral.com
>>
>> 2010/2/5 Inge Solvoll <in...@gmail.com>
>>
>> > Hi!
>> >
>> > I find myself repeating myself. Quite often when creating a page or a
>> > component, I create a companion javascript file with the same name as my
>> > component class. I got the idea today to try to create some kind of
>> worker
>> > or service that automatically includes a javascript that has the same
>> name
>> > as the component class and exists in the same folder on the classpath.
>> > After
>> > all, this is how the tml and properties files are hooked up to component
>> > classes, so why not do it for javascript as well?
>> >
>> > Any pointers to get me started? :)
>> >
>>
>>
>>
>> --
>> Regards,
>> Christophe Cordenier.
>>
>> Developer of wooki @wookicentral.com
>>
>
--
--
http://www.bodylabgym.com - a private, by appointment only, one-on-one
health and fitness facility.
--
http://www.ectransition.com - Quality Electronic Cigarettes at a
reasonable price!
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Automatically include javascript companion
Posted by Inge Solvoll <in...@gmail.com>.
Thanks!
I looked into this and it wasn't too hard to figure out (as usual).
But in the end, i realized that it's a silly idea that only leads to other
people having even more problems finding out what's going on in my code :) I
can afford the typing required to include the javascript companion for my
components :)
On Fri, Feb 5, 2010 at 10:05 AM, cordenier christophe <
christophe.cordenier@gmail.com> wrote:
> Hi,
>
> Your worker should use AssetSource and RenderSupport to automatically add
> the js file if it exists, have a look at IncludeJavaScriptLibraryWorker
> source file to see an exemple of script injection.
> BTW in my experience, the multiplication of JS/CSS files is not really
> appreciated by IE, so if you decide to work this way i advise you to also
> turn on (default) combine-scripts feature.
>
> Regards,
> Christophe Cordenier
>
> Developer of wooki @wookicentral.com
>
> 2010/2/5 Inge Solvoll <in...@gmail.com>
>
> > Hi!
> >
> > I find myself repeating myself. Quite often when creating a page or a
> > component, I create a companion javascript file with the same name as my
> > component class. I got the idea today to try to create some kind of
> worker
> > or service that automatically includes a javascript that has the same
> name
> > as the component class and exists in the same folder on the classpath.
> > After
> > all, this is how the tml and properties files are hooked up to component
> > classes, so why not do it for javascript as well?
> >
> > Any pointers to get me started? :)
> >
>
>
>
> --
> Regards,
> Christophe Cordenier.
>
> Developer of wooki @wookicentral.com
>
Re: Automatically include javascript companion
Posted by cordenier christophe <ch...@gmail.com>.
Hi,
Your worker should use AssetSource and RenderSupport to automatically add
the js file if it exists, have a look at IncludeJavaScriptLibraryWorker
source file to see an exemple of script injection.
BTW in my experience, the multiplication of JS/CSS files is not really
appreciated by IE, so if you decide to work this way i advise you to also
turn on (default) combine-scripts feature.
Regards,
Christophe Cordenier
Developer of wooki @wookicentral.com
2010/2/5 Inge Solvoll <in...@gmail.com>
> Hi!
>
> I find myself repeating myself. Quite often when creating a page or a
> component, I create a companion javascript file with the same name as my
> component class. I got the idea today to try to create some kind of worker
> or service that automatically includes a javascript that has the same name
> as the component class and exists in the same folder on the classpath.
> After
> all, this is how the tml and properties files are hooked up to component
> classes, so why not do it for javascript as well?
>
> Any pointers to get me started? :)
>
--
Regards,
Christophe Cordenier.
Developer of wooki @wookicentral.com