i've not tested your example but by reading the code i would say it should work 
like this conceptually.

i think there is no "community consensus" on using the JS use API or not. i 
personally would advise to not use it for non-trivial tasks, because it creates 
a unhealthy combination of javascript and java objects within the JS logic with 
often unexpected results even on simple thinks like string operations because 
you often do not know exactly if you have a js stirng or a java string object. 
it's also quite difficult to debug in this cases. my recommendation is to go 
with sling models and unit tests.

stefan


>-----Original Message-----
>From: lancedolan [mailto:lance.do...@gmail.com]
>Sent: Wednesday, January 11, 2017 2:30 AM
>To: users@sling.apache.org
>Subject: JS Use API usability or limitations
>
>I've got years of experience writing backing beans for Sling (aem)
>components, but can't seem to get a simple JS backing working using the
>new
>Javascript Use API.
>
>All I want to do is print out a resource's children resources.
>
>My JS backing bean:
>
>use([], function () {
>
> var returnObj = {
>        content: ""
>    };
>
>  var parent = resource.parent;
>  var children = parent.getChildren();
>
>  for (res in Iterator(children)) {
>       returnObj.content += res.name;
>  }
>
>    return returnObj;
>});
>
>The contents of returnObj.content will be the word "undefined" repeated
>X
>times, where X is the number of children nodes of parent. This tells me
>it
>successfully iterates the Iterable once for each item in it, but it's
>failing to give me a reference to a child. If I try res.getName()
>instead, I
>get:
>
>org.mozilla.javascript.EcmaError: TypeError: Cannot find default value
>for
>object
>
>In fact, every single variation I've tried for iterating that collection
>has
>lead to that exact Exception.
>
>It seems to me Rhino is getting in the way. Am I right? What is the
>community consensus on use of the JS Use API? Is it production-ready and
>stable? Is my problem a user-error? What limitations exist here? I was
>planning on doing the same things in JS that I've always done in Java,
>which
>is usually a lot of read/write operations on the JCR.
>
>
>
>--
>View this message in context: http://apache-
>sling.73963.n3.nabble.com/JS-Use-API-usability-or-limitations-
>tp4069490.html
>Sent from the Sling - Users mailing list archive at Nabble.com.


Reply via email to