cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript commands ignore if wrongly placed

simon
Champ in-the-making
Champ in-the-making
Hi,

I ran into a small bug (if I'm not mistaken) in the JavaScript API. I tried to change the name of my copied document but if you place the copy.name command above the if statement the copied document keeps the original name. If you place it right before the copy.safe command everything works. Can anyone confirm this?

Works fine:
if ( !copy.hasAspect("cm:generalclassifiable") ) {
   copy.addAspect("cm:generalclassifiable");
}
copy.name = "someNewName";
copy.save();

Does NOT work:
copy.name = "someNewName";
if ( !copy.hasAspect("cm:generalclassifiable") ) {
   copy.addAspect("cm:generalclassifiable");
}
copy.save();
3 REPLIES 3

kevinr
Star Contributor
Star Contributor
Thanks for reporting this, i'll take a look.

Cheers,

Kevin

kevinr
Star Contributor
Star Contributor
OK I know why this is - it's a "feature" that i'll need to document so you can work around it as you have done in your example.

Most of the Node operations cause any cached values or values that have not yet been set (e.g. properties) to be reset. So the addAspect() method will cause the properties collection to be cleared on the assumption that adding the aspect has "changed" the node. Therefore you need to save() your properties immediately after setting them i.e. before performing any other operations such as copy/move/addaspect etc. In your case it is more subtle because the node.name property is really just a shortcut to node.properties.name - so it is not obvious that a property has been modified when you change the name.

It may be better for me to force a save() operation when you set .name directly using the shortcut, but we don't want to set every property after each property.x=y call as this would give weaker performance which is why the .save() method exists.

Thanks,

Kevin

simon
Champ in-the-making
Champ in-the-making
Thanks for the fast reply Kevin. The workaround works fine but all these small annoyances cost some time to find out why some code isn't working. Hope these will disappear when Alfresco gets more "mature".
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.