You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Sandro Boehme <sa...@gmx.de> on 2015/10/22 20:43:19 UTC
Sightly :: JavaScript Use-API :: passing an element attribute to JS
file
Hello,
for the Sling Sitebuilder I would like to pass an id that is specified
as an element attribute to the Sightly server side JavaScript file
defined by the JS Use-API. Is that possible?
This example works:
<sly class="component" data-component-type="bs-p" data-component-id="4"
data-sly-use.componentResource="${'component.js' @id='4'}">
...
</sly>
component.js:
"use strict";
use(function () {
return resource.getChild(this.id);
});
content:
{
"jcr:primaryType":"sling:Folder",
"4":{
"jcr:primaryType":"nt:unstructured",
"value":"foo bar"
}
}
But in the 'use' block of the 'sly' tag I specify the '4' as literal
(@id='4'). Instead I would like to reference it e.g. like
@id=attributes.data-component-id. Is that possible?
Thanks in advance for any help!
Best,
Sandro
Re: Sightly :: JavaScript Use-API :: passing an element attribute to
JS file
Posted by Sandro Boehme <sa...@gmx.de>.
Hi Vlad,
thanks for your answer again.
The "4" is indeed a literal. But I'm thinking about an other way of
handling that.
Best,
Sandro
Am 23.10.15 um 08:59 schrieb Vlad Bailescu:
> Hi Sandro,
>
> Sightly blocks/expressions are only aware of the HTML context, not the
> whole DOM tree so you cannot use the attributes like this.
>
> However, I assume data-component-id="4" is generally not hardcoded, but
> rather coming from some sort of expression, like
> data-component-id="${some_expression}". In this case you can pass the same
> expression to the Use-Object initialization: @id=${some_expression}
>
> Vlad
>
> On Thu, Oct 22, 2015 at 9:43 PM, Sandro Boehme <sa...@gmx.de> wrote:
>
>> Hello,
>>
>> for the Sling Sitebuilder I would like to pass an id that is specified as
>> an element attribute to the Sightly server side JavaScript file defined by
>> the JS Use-API. Is that possible?
>>
>> This example works:
>>
>> <sly class="component" data-component-type="bs-p" data-component-id="4"
>> data-sly-use.componentResource="${'component.js' @id='4'}">
>> ...
>> </sly>
>>
>> component.js:
>> "use strict";
>> use(function () {
>> return resource.getChild(this.id);
>> });
>>
>> content:
>> {
>> "jcr:primaryType":"sling:Folder",
>> "4":{
>> "jcr:primaryType":"nt:unstructured",
>> "value":"foo bar"
>> }
>> }
>>
>>
>> But in the 'use' block of the 'sly' tag I specify the '4' as literal
>> (@id='4'). Instead I would like to reference it e.g. like
>> @id=attributes.data-component-id. Is that possible?
>>
>> Thanks in advance for any help!
>>
>> Best,
>>
>> Sandro
>>
>
Re: Sightly :: JavaScript Use-API :: passing an element attribute to
JS file
Posted by Vlad Bailescu <vl...@bailescu.ro>.
Hi Sandro,
Sightly blocks/expressions are only aware of the HTML context, not the
whole DOM tree so you cannot use the attributes like this.
However, I assume data-component-id="4" is generally not hardcoded, but
rather coming from some sort of expression, like
data-component-id="${some_expression}". In this case you can pass the same
expression to the Use-Object initialization: @id=${some_expression}
Vlad
On Thu, Oct 22, 2015 at 9:43 PM, Sandro Boehme <sa...@gmx.de> wrote:
> Hello,
>
> for the Sling Sitebuilder I would like to pass an id that is specified as
> an element attribute to the Sightly server side JavaScript file defined by
> the JS Use-API. Is that possible?
>
> This example works:
>
> <sly class="component" data-component-type="bs-p" data-component-id="4"
> data-sly-use.componentResource="${'component.js' @id='4'}">
> ...
> </sly>
>
> component.js:
> "use strict";
> use(function () {
> return resource.getChild(this.id);
> });
>
> content:
> {
> "jcr:primaryType":"sling:Folder",
> "4":{
> "jcr:primaryType":"nt:unstructured",
> "value":"foo bar"
> }
> }
>
>
> But in the 'use' block of the 'sly' tag I specify the '4' as literal
> (@id='4'). Instead I would like to reference it e.g. like
> @id=attributes.data-component-id. Is that possible?
>
> Thanks in advance for any help!
>
> Best,
>
> Sandro
>