Pass arguments to JavaScript function from Sightly - aem

I can call JavaScript functions from Sightly with no parameters, e.g.
<div
data-sly-use.ResourceUtils="/libs/wcm/foundation/components/utils/ResourceUtils.js"
data-example="${ResourceUtils.aFunction}" />
But I would like to call a function called 'getResource' that accepts 1 argument.
How can I address this function from Sightly, passing in this argument?

basically, you can access arguments by using "this.", for example:
Your html would look something like this:
<div data-sly-use.ResourceUtils="${'/libs/wcm/foundation/components/utils/ResourceUtils.js' # arg='argument'}" data-example="${ResourceUtils.aFunction}" />
And the JS would be:
use( function () {
var argument = this.arg;

Related

How to access a string array using Sightly(HTL)

How can i access a string array coming from a model class using sightly(HTL)
The TestModel is a model class that returns a string array , getResult() is the getter used to return the string array
how can I use sightly to get it??
<p>display output :</p>
<sly data-sly-use.object = "com.silversea.core.models.TestModel">
<sly data-sly-list.mylist = "${object.Result}"> //what command show we use instead of data-sly-list
<p>1st text: ${item} </p>
</sly>
</sly>
The problem you are facing here is caused by two things:
Defining an identifier on the data-sly-list statement allows you to rename the itemList and item variables. item will become variable and itemList will become variableList
More details in https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html
So in your example you must change ${item} into ${mylist}
<p>display output :</p>
<sly data-sly-use.object = "com.silversea.core.models.TestModel">
<sly data-sly-list.mylist = "${object.result}"> //what command show we use instead of data-sly-list
<p>1st text: ${mylist} </p>
</sly>
</sly>
The second thing is that you should also follow the java bean naming convention: So if you have a getter getResult() then in HTL you should use ${object.result} (starting from lowercase)

How to pass parameter to a java function in JSP scriptlet?

I want to pass the value of assembler.area.id, sent by controller to the java function getIsAreaWaitTimeActiveByAreaId(), inside scriptlet. I am not sure how to do so. getIsAreaWaitTimeActiveByAreaId() takes in Long.
<c:set var="isWaitTimeActive" value="${assembler.area.id}" scope="page"/>
<%#page import="com.ihc.wtrack.service.impl.ReportServiceImpl"%>
<%
ReportServiceImpl reportController = new ReportServiceImpl();
boolean isActive = reportController.getIsAreaWaitTimeActiveByAreaId(isWaitTimeActive);
%>
Thanks.

Binding an html form action to a controller method that takes some parameters

In my Find controller I have a method like:
public Result findLatest(String repoStr) {
............
}
Which is linked through a route:
GET /latest controllers.Find.findLatest(repo: String)
Then, I have a form in a view like:
<form action="#routes.Find.findLatest()" method="get">
....
<select name="repo">....</select>
</form>
But obviously that is failing, because it is expecting some parameters that I do not fulfill in the action. What is the correct way to do this without having to end up leaving the findLatest method taking no parameters in my controller?
You could change the routes to accept an empty string:
GET /latest/:repo controllers.Find.findLatest(repo: String = "")
Then configure your controller function to handle empty string.
That way,
<form action="#routes.Find.findLatest()" method="get">
....
<select name="repo">....</select>
will evaluate repo as an empty string at the controller level.
Edit: Support for this implementation was dropped in Play v 2.1
You may be interested in Play's Optional parameters e.g. play.libs.F.Option[String]
Example: How to handle optional query parameters in Play framework
GET /latest/:repo/:artifact controllers.Find.findLatestArtifact(repo: play.libs.F.Option[String], artifact: play.libs.F.Option[String])
This will allow you flexibility in which arguments need to be provided.
Not sure which language you're using but the link above contains an example for scala and the method declaration in java would look something like:
import play.libs.F.Option;
public static Result findLatestArtifact(Option<String> repo, Option<String> artifact){ ... }
and updated implementation 2.1
Routes with optional parameter - Play 2.1 Scala
EDIT: play 2.1+ Support : Props to #RobertUdah below
Initializing to null:
GET /latest/ controllers.Find.findLatest(repo: String = null)
GET /latest/:repo controllers.Find.findLatest(repo: String)
<form action="#routes.Find.findLatest()" method="get">
Normally all form data go in the body and you can retrieve them in your action method with bindFromRequest() (see docs).
If you really want to pass one form element as a part of the URL then you have to dynamically compose your URL in JavaScript and change your route.
Your route could look like:
GET /latest/:repo controllers.Find.findLatest(repo: String)
And the JavaScript part like (I didn't actually test the code):
<form name="myform" action="javascript:composeUrl();" method="get">
....
<select name="repo">....</select>
</form>
<script>
function submitform() {
var formElement = document.getElementsByName("myform");
var repo = formElement.options[e.selectedIndex].text;
formElement.action = "/lastest/" + repo;
formElement.submit();
}
</script>
Cavice suggested something close to what I consider the best solution for this (since F.Option are not supported anymore with the default binders in Play 2.1 ).
I ended up leaving the route like:
GET /latest controllers.Find.findLatest(repo=null)
and the view like:
<form action="#routes.Find.findLatest(null)" method="get">
<select name="repo"> .... </select>
....
</form>
and in the controller:
public Result findLatest(String repoStr) {
if(repoStr==null) {
repoStr=Form.form().bindFromRequest().get("repo");
.....
This allows me to have a second route like:
GET /latest/:repo controllers.Find.findLatest(repo: String)

Reset angular form with $setPristine

Using $scope.form.$setPristine() returns undefined.
<html>
<form name="my_form">
...
</form></html>
Defining the controller on my $routeProvider
.when('page_name', {
templateUrl: '...',
controller: function($scope) {
console.log(my_form); // returns the form object
my_form.$setPristine(); returns Object #<HTMLFormElement> has no method '$setPristine'
console.log($scope.my_form); // returns undefined
$scope.my_form.$setPristine(); // returns Cannot call method '$setPristine' of undefined
}
}
I've also tried passing the form to the $scope via jQuery $scope.my_form = $('form'); which, as expected, just sent the form object and resulted in the first error. What does it take to get this method to work?
I tried putting a Plunker together to demonstrate but I can't seem to get routing to work on there properly.
Everything else about the form works btw. I would just like to know how to get $setPristine to work.
Put the form and the ng-controller at the same level...
<form name="my_form" ng-controller="my_controller">
That's working for me.
(sorry about my english) ;-)

Bind with coffeescript

How can I call native bind method of function-object with coffeescript ? This is the example of what I am trying to achieve:
window.addEventListener("load",function(e){
this._filter(true);
}.bind(this);
)
Just add some parentheses around the function so that you can .bind the right thing:
window.addEventListener('load', ((e) ->
this._filter(true)
).bind(this))
That will use the native bind method instead of the usual var _this = this trickery that CoffeeScript's => uses.