[ 
https://issues.apache.org/jira/browse/SLING-77?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Lars Trieloff updated SLING-77:
-------------------------------

    Attachment: sling-77.patch

This patch adds host objects for Resource and for Node. The Resource API allows 
you to get a Javascript representation of the Node using the resource.item 
property. A script can then iterate over all properties and child nodes of a 
resource as if it were a hashmap (or any Javascript object). In order to list 
all properties of a node, following script could be used:

<%-- microsling ESP template example, store this as html.esp --%>
<html>
<body>
<p>This page is generated from an ESP template!</p>
<h1><%= resource.uri %></h1>
<ol>
<% 
for (var prop in resource.item) {
    %><li><strong><%= prop %></strong>: 
    <% if (resource.item[prop].substr) { 
        //this is a string, just show it
       %><%= resource.item[prop] %><%
    } else {
        //this is an array (multi-value-property), loop over it
        for (var val in resource.item[prop]) {
            %><span><%= resource.item[prop][val] %></span> <%
        }
    } %></li><%
}
%></ol>
<a href="/microsling<%= resource.uri  %>.edit.html">Edit</a>
</body>
</html>

this example also shows how to deal with multi-value properties that will be 
represented as a javascript array of strings. Same-name child nodes will be 
represented as node objects in an array. If you have a property and a 
child-node of the same name, the array will contain a string and a node.

Using following code you can iterate over a number of children and create 
something like a directory listing: (saved as 
sling/scripts/nodetypes/nt/unstructured/html.esp)

<%-- microsling ESP template example, store this as html.esp --%>
<html>
<body>
<p>This directory listing is generated from an ESP template!</p>
<h1><%= resource.uri %></h1>
<ol>
<% 
for (var prop in resource.item) {
    if (resource.item[prop]["text"]) {
        %><li><a href="/microsling<%= resource.item[prop] %>.html"><%= 
resource.item[prop] %></a></li><%
    }
}
%>
</ol>
</body>
</html>

> Provide an easy-to-use Javascript API for JCR
> ---------------------------------------------
>
>                 Key: SLING-77
>                 URL: https://issues.apache.org/jira/browse/SLING-77
>             Project: Sling
>          Issue Type: Improvement
>          Components: microsling
>            Reporter: Lars Trieloff
>         Attachments: sling-77.patch
>
>
> As discussed here, I propose a Javascript-tailor-made wrapper API to make 
> JCRNodes more accessible to script and template authors
> http://thread.gmane.org/gmane.comp.apache.sling.devel/491

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to