cancel
Showing results for 
Search instead for 
Did you mean: 

Access model variable from dashlet JavaScript

sokolko
Champ in-the-making
Champ in-the-making
Hello all! I'm developing a custom dashlet for Alfresco share. I've seen many examples and made several simple projects, but now I need something more complicated. I have a project (it's a set of files). Some of them contain JS code.
My idea is to make a query to a webscript in <Project home>/config/alfresco/site-webscripts/org/alfresco/components/dashlets/mydashlet.get.js, store its result in model variable and then access it in <Project home>/source/web/components/dashlets/mydashlet.js
I try to do it like this:
1) <Project home>/config/alfresco/site-webscripts/org/alfresco/components/dashlets/mydashlet.get.js

function main()
{
      var json = remote.call("/api/task-instances?state=completed");
         if (json.status == 200)
         {
            data = eval('(' + json + ')');
         }
   
      model.data = data.data;
}
main();
2)<Project home>/source/web/components/dashlets/mydashlet.js


loadData: function MyDashlet_loadData()
      {
         alert(model.data);
      }


Nothing happens and I don't see my message… Any help or ideas really appreciated: I've been trying to do it for a week…
P.S. In my <Project home>/config/alfresco/site-webscripts/org/alfresco/components/dashlets/mydashlet.get.html.ftl I have access to ${data} from my model.data
2 REPLIES 2

angello0571
Champ on-the-rise
Champ on-the-rise
Hello,

Rember that there are two types of webscripts, the first type is to retrieve data vía REST, or webscripts for show information (for example dashlets). The behaivor is based in MVC pattern, in this case your Controller could be a js file or a Java class to access the Model. Your view is in freemarker. There is good information about webscripts and MVC pattern in the Wiki and Web.

But If you have a webscript to retrieve data, and I guess for your code that the response is in JSON format, I recomend you to do the next in your freemarker template:

Alfresco.util.Ajax.request(
         {
            method: "POST",
            url: "/alfresco/service/com/someservice",
            dataObj:
            {
               var1:'value1',
               var2:'value2',
            },
             successCallback:
            {
                fn: function dlA_onActionDetails_refreshSuccess(response)
                    {
                  var rawResponse = response.serverResponse.responseText+"";
                  var obj = eval('(' + rawResponse + ')');
                  //to do something with this object…
               }       
            }
         });

After that you can play with that object as you want!

You can access vía GET or POST, but it depends of the definition of your webscript.

Good Luck

sokolko
Champ in-the-making
Champ in-the-making
Thanks for your help! It did give me a new amazing idea Smiley Happy I used my ftl file, where I had access to all model variable contents to pass the necessary parameters to my js-class-file and it all worked fine. Now the code (just in case anyone needs some examples like this):
1) mydashlet.get.html.ftl:


<script type="text/javascript">//<![CDATA[
   var d=new Alfresco.dashlet.MyDashlet("${args.htmlid}").setOptions(
   {
      "componentId": "${instance.object.id}",
   }).setMessages(
      ${messages}
   );
   <#list data as item>
         d.setData("${item.id}", "${item.name}", "${item.title}", "${item.state}", "${item.properties.bpm_description}");
   </#list>
  
//]]></script>

2) mydashlet.js


      setData: function MyDashlet_setData(id, name, title, state, description)
      {
         //just passing data from view to my global array
         var o = new Object();
        
         o.id=id;
         o.name=name;
         o.title=title;
         o.state=state;
         o.description=description;
         tasks.push(o);
      },

So after all I have all I need in my global array and can use it whenever I want.