cancel
Showing results for 
Search instead for 
Did you mean: 

how to write data to an aspcet using java script

friedman30
Champ in-the-making
Champ in-the-making
hi,

i have added a custom aspect:

<aspect name="custom:ThumbnailAspect">
           <title>Thumbnail Aspect</title>
            <associations>
             <association name="custom:Thumbnail">
                <title>Thumbnail</title>
                <source>
                 <many>true</many>
              </source>
                <target>
                   <class>cm:content</class>
                   <mandatory>false</mandatory>
                         <many>false</many> <!– allow only one thumbnail to be added –>
                   </target>
               </association>
          </associations>
         </aspect>
now, i need to add the content of this aspect (which will be a link to another file) using a java script.

i am running this script:
var props = new Array(1);
          props["custom:Thumbnail"] = PATH;
          var added=  document.addAspect("custom:ThumbnailAspect", props);

where document is the content that holds the aspect, and PATH is the value i want to add.
after running it, the value of added is true (and i do have a new thumbnail value added to the content), but the value (PATH) is still empty….

i can't find a way to set the value for an aspect…

any ideas???  :shock:
10 REPLIES 10

kevinr
Star Contributor
Star Contributor
In Alfresco 2.0 the JavaScript API does not have the capability to create/modify/remove target associations. You can add/remove aspects with properties as you have found, but if those aspects model associations then you will not be able to do a lot. The good news is that this feature has already been added to the JavaScript API for Alfresco 2.1 and is available now to try on a nightly-build or in the HEAD codebase from our public SVN server.

For now, you could model the thumbnail differently. If your aspect just uses a noderef property to point to the ref of the node that you want, you can then easily modify this using javascript, for example:

<aspect name="custom:ThumbnailAspect">
   <title>Thumbnail Aspect</title>
   <properties>
      <property name="custom:Thumbnail">
         <title>Thumbnail</title>
         <type>d:noderef</type>
         <mandatory>false</mandatory>
      </property>
   </properties>
</aspect>
Your code (assuming PATH=some node) will then work.

Thanks,

Kevin

friedman30
Champ in-the-making
Champ in-the-making
I have noticed one more thing.

If i run the script on a content that has the thumbnail aspect with a value,
when i do this:

var assocs = document.assocs["custom:Thumbnail"];
var testFile = currentFolder.childByNamePath("contentFPKX.jpg"); //a different content…
if (assocs != null)
  {
   assocs[0] = testFile;
  }

i see that assocs[0] is changing to the testFile value, but it is not saved anywhere…. (not saved to the DB :?: ???? :?Smiley Happy

at the JavaScript API it says that assocs returns a read only value…
is there a way to bypass this??

thanks,

Dror

kevinr
Star Contributor
Star Contributor
i see that assocs[0] is changing to the testFile value, but it is not saved anywhere…. (not saved to the DB :?: ???? :?Smiley Happy

at the JavaScript API it says that assocs returns a read only value…
is there a way to bypass this??

There is no way to "bypass" it - as i said in my reply above, the JavaScript cannot create/modify associations in 2.0.

Kevin

friedman30
Champ in-the-making
Champ in-the-making
Thanks Kevin,

I've tested your solution using:
<type>d:noderef</type>

Now, when i add the aspect, i can see the thumbnail property, but is is a read-only field… (i believe i can edit it using java-script).
(i get this message from the server:
WARN  [component.property.UIProperty] Setting property {custom.model}Thumbnail to read-only as it can not be edited
)
The problem is that i need to leave the users the the ability to modify it via the usual UI as well.

To be more specific - the thing i need is a thumbnail field that can be edited in 2 ways:
1) java-script - will be used by the admin of the system
2) alfresco UI - for users of Alfresco (the best solution would be to have a component that combines a textBox and a button, that opens a dialog-box from which the user will choose the thumbnail).

Is there any way to do that in Alfresco 2.0?

also, you wrote that it is possible to modify target associations in Alfresco 2.1 JavaScript API, i am now working with the 2.0 version (rev. 569) of Alfresco. what should i download if i want to use the 2.1 JavaScript API with this revision?

:idea: (is there a way to modify target associations (in version 2.0) using the Java API?)

THANKS!!

Dror

kevinr
Star Contributor
Star Contributor
A d:noderef value cannot be editing in the UI at present. The problem you have is that if you use an association; you can edit it in the UI, but you cannot modify using Alfresco 2.0 JavaScript API, but if you use a d:noderef property instead; then you can modify it using the 2.0 JavaScript API but you cannot edit it in the UI.

>(is there a way to modify target associations (in version 2.0) using the Java API?)
>Is there any way to do that in Alfresco 2.0?
No and no, not if you want to modify it with the JavaScript API.

>you wrote that it is possible to modify target associations in Alfresco 2.1 JavaScript API, i am now working with the 2.0 version (rev. 569) of Alfresco. what should i download if i want to use the 2.1 JavaScript API with this revision?

Take a nightly build is the easiest route:
http://dev.alfresco.com/downloads/nightly/dist/

Thanks,

Kevin

friedman30
Champ in-the-making
Champ in-the-making
thanks Kevin.
:!:

can you be more specific ?
where in the new API should i look and what exactly should i look for?

kevinr
Star Contributor
Star Contributor
I haven't had time to write the wiki docs yet (they will be done ready for the 2.1 release), for now here some info on what you can do with associations using the JavaScript/FreeMarker APIs in 2.1:

JavaScript API

Target assocs (non-child):
var targetNodes = node.assocs["cm:assoctype"];
node.removeAssociation(targetNode, "cm:assoctype");
node.createAssociation(targetNode, "cm:assoctype");

Child assocs:
var childNodes = node.childAssocs["cm:childassoctype"];
var child = node.createNode("name", "cm:nodetype", "cm:childassoctype");


FreeMarker API

Target assocs (non-child):
<#list node.assocs["cm:assoctype"] as t>

Child assocs:
<#list node.childAssocs["cm:childassoctype"] as c>

rgauss
Champ in-the-making
Champ in-the-making
Hope this isn't off topic as it has nothing to do with javascript and I'm not sure of exactly what you're trying to do but there is an existing thumbnails forge project:

http://forge.alfresco.com/projects/thumbnails/

shay
Champ in-the-making
Champ in-the-making
Hi,

Is it possible In Alfresco 2.0 Java API to create/modify/remove target associations?

Shay