Hey community,
I'm trying to write a user macro, which adds the id of the current page to all sub pages via jQuery.
The current code looks like this:
#set($label="$content.id")
<script>
AJS.toInit(function() {
AJS.$('#updatetm').click(
function() {
#foreach ( $child in $content.getChildren() )
jQuery.post(contextPath + "/json/addlabelactivity.action",
{"entityIdString": "$child.id", "labelString": "$label", "atl_token": jQuery("#atlassian-token").attr("content") },
function() { console.log("$child.id updated!"); });
#end
});
});
</script>
The code itself gets called by clicking an aui-button:
<button class="aui-button aui-button-primary" id="updatetm">Update Pages</button>
My problem at the moment is, that the content within the foreach loop changes during the execution.. it even changes outside the script section (rendering the $label variable useless).
I've ran a test with 5 cascaded sub pages:
Test (main page - ID: 25166773)
|__Test FP (ID: 25166775)
| |__Test SP (ID: 25166778)
|
|__Test FP2 (ID: 25166782)
| |__Test SP2 (ID: 25166784)
|
|__Test SP3 (ID: 25166780)
The test (and the logs on the console) shows that - reaching the second level in hierarchy - the content switches from the initial parent page to the "current" parents:
Current child: 25166775
Label: 25166773
Content ID: 25166773
Current child: 25166780
Label: 25166773
Content ID: 25166773
Current child: 25166782
Label: 25166773
Content ID: 25166773
Current child: 25166778
Label: 25166775
Content ID: 25166775
Current child: 25166784
Label: 25166782
Content ID: 25166782
Any ideas why this is happening and how to prevent it? Is my jQuery wrong? Every idea and/or help is appreciated.
Cheers,
Dom