This simple use case has been taken from AEM community, I thought an article for this would really help people who are looking for this or may need in future.
We have two field, one is pathfield (which helps to browse repository) and another a tag input field. Author selects an asset from pathfield and based on the the selection Tag input field is populated with tags available for selected assets.
Select an asset from pathfield
Select different asset from pathfield, tags gets updated.
Code Look Up
To make this work, you simply need to add a widget listener to the asset pathfield. Listener will listen for event dialogclose which means when you select an asset from pathfield browse asset dialog and click ‘Ok’ will cause the event to trigger.
In above image it can be seen that dialog close listener has been added to asset pathfield.
path = this.value+"/jcr:content/metadata.json";
var x = CQ.shared.HTTP.eval(path);
var tags = ;
tags = x['cq:tags'];
dialog = this.findParentByType('dialog');
field = dialog.getField('./tags');
We simply create complete path where tags are present for particular asset and get the response on JSON. In complete JSON response we have many properties values and one of them is our property name cq:tags. In line we are getting the value for cq:tags and setting this value to tag input field.
Comment below if you need any help or have questions.