Iterate assocs in javascript to remove them
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2010 11:07 AM
How do I iterate an associations of a specified type in javascript in order to remove them?
I've tried this (and some variants), but it does not work.
So what is the correct way?
I've tried this (and some variants), but it does not work.
for (i in fileNode.assocs["xel:reviewedby"]){ fileNode.removeAssociation(fileNode.assocs["xel:reviewedby"][i], "xel:reviewedby"); }
So what is the correct way?
Labels:
- Labels:
-
Archive
4 REPLIES 4

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2010 07:56 PM
Hi,
In your for…in loop the variable "i" is set to the value of the object within the array being iterated over, not the current index in the array.
Try this ….
In your for…in loop the variable "i" is set to the value of the object within the array being iterated over, not the current index in the array.
Try this ….
for (var targetNode in fileNode.assocs["xel:reviewedby"]){ fileNode.removeAssociation(targetNode, "xel:reviewedby"); }
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2010 02:24 AM
Thanks for the reply.
It doesn't work, have created this test code
When this code is run, it throws error (partial)
I tried to change to sourceNode.removeAssociation(targetNode.child, "cm:attachments");
but that didn't work either.
Running this code on 3.3g
It doesn't work, have created this test code
var namesuffix = userhome.children.length; //to make file name uniquevar sourceNode = userhome.createNode("testassocsfile"+namesuffix +".txt", "cm:content");sourceNode.content = "the original text";sourceNode.addAspect("cm:attachable");logger.log("LX Created source node");var destNode = userhome.createNode("attached1-"+namesuffix +".txt", "cm:content");destNode.content = "Text in attached document";logger.log("LX Created dest node 1");sourceNode.createAssociation(destNode, "cm:attachments");logger.log("LX Created assoc destNode to sourceNode");var destNode2 = userhome.createNode("attached2-"+namesuffix +".txt", "cm:content");destNode2.content = "Text in attached document2";logger.log("LX Created dest node 2");sourceNode.createAssociation(destNode2, "cm:attachments");logger.log("LX Created assoc destNode2 to sourceNode");sourceNode.save();for (var targetNode in sourceNode.assocs["cm:attachments"]){ logger.log("LX Removing " + targetNode) //targetNode returns integer here logger.log("LX Removing name " + targetNode.name) //returns undefined sourceNode.removeAssociation(targetNode, "cm:attachments"); }
When this code is run, it throws error (partial)
Caused by: org.alfresco.error.AlfrescoRuntimeException: 08210080 Can't find method org.alfresco.repo.jscript.ScriptNode.removeAssociation(string,string). (file:/opt/alfresco/xelerated/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/test/lxtest.get.js#30)
As you can see in my test code, the logging of targetNode in the iteration returns an integer, starting at 0 (and why I tried to use it the way I did, I think…).I tried to change to sourceNode.removeAssociation(targetNode.child, "cm:attachments");
but that didn't work either.
Running this code on 3.3g

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2010 02:43 AM
Hi,
Yes, my bad. The form you where using does indeed return the index of the array being iterated over …
I need to go back to JavaScript school!
Cheers,
Roy
Yes, my bad. The form you where using does indeed return the index of the array being iterated over …
// example iterating array indexes using 'for .. in'var out1 = "";for (i in userhome.children){ out1 += userhome.children[i].name + "<br>";}// example iterating array values using 'for each .. in'var out2 = "";for each (n in userhome.children){ out2 += n.name + "<br>";}
I need to go back to JavaScript school!
Cheers,
Roy
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2010 02:56 AM
OK,
this is how you do it for my example code
Your javascript skills clearly exceed mine, and I'll give you your second rating point
this is how you do it for my example code
for each (targetNode in sourceNode.assocs["cm:attachments"]){ sourceNode.removeAssociation(targetNode, "cm:attachments"); }
Your javascript skills clearly exceed mine, and I'll give you your second rating point

