Error saving this JS in WSO2-Integration Studio - javascript

I use Integration Studio,
and I have always used inside my Xml files js script,
But this time I still get an error saving this inline JS, and I really can't understand the reason of this error.
<script language="js"><![CDATA[var elements = mc.getProperty('elementsFromJson');
var payload = [];
for(let i = 0; i <= elements.length; i++){
var id= elements[i].id;
var type= elements[i].type;
var couple= {"id":id ,"type":type};
print(couple);
payload.push(couple);
}
var payloadFinale = {payload};
print('result in JSON: ' + payloadFinale);
var data= mc.setPayloadJSON(payloadFinale);]]></script>
So would be great your help in case you use Integration studio as me,
thank you for your time.

You have issues in the following lines.
for(let i = 0; i <= elements.length; i++){
Here change let => var
var payloadFinale = {payload};
The above syntax is wrong, not sure what you are trying to achieve here. Probably, you can remove the above line, when you do mc.setPayloadJSON it would be saved as a proper JSON.

Related

get params from a js function from a webpage

I'm trying to retrieve automatically some element onto a webpage, but I'm stuck with multiple things.
First I'm looping through all the classes named with the help of document.getElementsByClassName('trList') that got the informations that i need.
Then into the multiple results i'm trying to extract the parameters contained in this :
<div id="action_0" onclick="cba.facture.controllers.factureListeController.createFactureListeTooltipActionDialog('73039508',
'action_0_TooltipDialog',
'action_0',
'190px', 'Télétransmise', '1', 'false', '0',
'7622', '14902', 'Télétransmise', '')" onmouseover="dijit.Tooltip.defaultPosition = ['above', 'below']">…</div>
I'm trying to get the 1st and 9th parameters of the onclick function.
And of course there are several iteration of this function in the results listed by the GetelementsByClassName.
I really don't have any clue on how to do this.
Could someone give me some direction on how to retrieve this?
Edit :
Trying to use the answer given earlier by #adriano009
i've got two problems :
First, the LET scope creates a bug in safari, telling me that i "can't create duplicate variable that shadows a global property”; so i'm using VAR for this one.
Second, with this code :
jsScript +="var slides = document.getElementsByClassName('trList');";
jsScript +="for (let i = 0; i < slides.length; i++) {";
jsScript += "let attr = slides[i].getAttribute('onclick'); ";
jsScript +=" console.log(attr);";
jsScript +="};";
Even though there are onclick attributes listed in slides.item(i), the log shows that it founds 0 attributes while trying to get them with slides[i].getAttribute('onclick'). Why's that?
let all = document.getElementsByClassName("class_name");
for (let i=0, i < all.length; i++) {
let attr = all[i].getAttribute("onclick");
let first_param = attr.split[','][0];
let ninth_param = attr.split[','][8];
console.log(first_param, ninth_param);
}

PhoneGap localStorage taking too long to access data

I have saved long string in localMemory named UserContacts, but it takes very long to access the data, here is the code that I am using:
var $Contacts = $('#Contacts');
var htmltext = window.localStorage.getItem("UserContacts");
$(htmltext).appendTo($Contacts);
$Contacts.listview("refresh").listview();
Any ideas how to improve performance for this?
P.S. I am developing application for android and using PhoneGap.
Thank you.
instread of storing html in localstorage you should save only data and then create your html using javascript, see below code
var data = window.localStorage.getItem("UserContacts");
var len = data.length;
var html = "";
for(var i=0; i< len;i++) {
html += $("<li/>").text(data[i]);
}
$("ul#yourId").append(html)

collect data by id using javascript

I have to save temporary data for my webpage using java script.This is the way that i save i one by one since the data is an array.
var product= new Array();
product[1] = document.getElementById("product[1]").value;
product[2] = document.getElementById("product[2]").value;
This method is working. but when i run it by looping, it doesnt work.
for(var i=1; i < no_item; i++){
product[i] = document.getElementById("product[i]").value;
}
*product[] is a varibale that I take from a html dropdown menu
Can anyone please tell me the problem ? thanks ~ =)
Should be written as, as you are going to be getting the id "product[i]" every time with your original code. This will get "product[1]" then "product[2]" and so on:
for(var i=1; i < no_item; i++){
product.push(document.getElementById("product[" + i + "]").value);
}
Also, as a comment, we tend to prefer var product = []; over var product = new Array(); in javascript but both will work.

convert js output to objective C

I am doing work on iphone app in which I have also used js as well as css files along with objective C to get better output. But at one point I stuck out. I am using the following js code which is showing the alert. I have attached the image also which is representing the alert by clicking on OK button.
**NSString *someHtmlContent1 = #"<script src='jquery.min.js'>
</script>
<script src='jquery-ui.min.js'></script>
<script src='jquery.ui.touch-punch.min.js'></script>
<script>
function result(qn) {
var u = document.getElementById('sortable'+qn);
var LIs = u.getElementsByTagName('li');
var list = [];
for( var i = 0; i < LIs.length; ++i ){
var LI = LIs[i];list.push(LI.innerHTML);
}
res = list;
var i=0;
var str = '';
for (i in res) {
str = str+' '+res[i];
}
alert(str);
return str;
}
</script>";**
I want to get the content**(.......)** in NSString which is showing on alert. Anyone know that how to convert js variables to objective C? Please do help me.
Thanks to all.
The only trick to make a link between JS and objective-C is the following one :
In your JS, call a link like window.location = "something://smth/smthelse"
And in the Objective-C, in your web view delegate method (webView:shouldStartLoadWithRequest:navigationType: ), you can handle this call and do something with it.

Can't set the value of input with Javascript

I'm trying to change the value of an input field with Javascript.
I tried everything, but nothing seems to works. I tried putting the 5 between quotation marks and using jquery. I also double-checked the array and everything.
Here is the input code:
<input type="number" id="id_[SOME_ID_HERE]" value="0">
and the loop used to update the values.
for (var i = 0; i < shoppingCart.length; i++) {
var val = shoppingCart[i];
document.getElementById("id_" + val.substring(3)).value = 5;
}
jsfiddle: http://jsfiddle.net/zkTud/
EDIT: Seems like it doesn't work with type="text" as well...
EDIT2: Thank you everyone who answered. My problem was actually something else.
The input was loaded from another page, and it took time and the for loop I had problem with (see above) was executed before the file was done loading.
All I did was to move the for loop as is to the callback function and it works now.
Thanks anyways!
I really appreciate the help I'm getting in this site! :)
The problem is that your call to substring is returning too much of the string, so there are no elements found by getElementById. Change it to this:
for(var i = 0; i < shoppingCart.length; i++) {
var val = shoppingCart[i];
document.getElementById("id_" + val.substring(5)).value = 5;
}
Here's an updated fiddle.
The substring method (when called with one argument) returns the characters from the index specified to the end of the string. Since you are specifying index 3, you get "d_1", "d_2" etc. when actually you just want the number.
Alternatively, you could of course change the string to which you append the substring, but I think that would be more confusing to read (not immediately obvious which element will be returned):
document.getElementById("i" + val.substring(3)).value = 5;
demo http://jsfiddle.net/bY4EV/6/
sustring(3) gives d_1 : How to substring in jquery
hope this helps
code
var shoppingCart = new Array();
shoppingCart[0] = "prod_1";
shoppingCart[1] = "prod_3";
shoppingCart[2] = "prod_2";
for(var i = 0; i < shoppingCart.length; i++) {
var val = shoppingCart[i];
$("#id_" + val.substring(5)).val("5");
}
​
Check this, JSFiddle , Updated and corrected your problem.
Code:
var shoppingCart = new Array();
shoppingCart[0] = "prod_1";
shoppingCart[1] = "prod_3";
shoppingCart[2] = "prod_2";
for(var i = 0; i < shoppingCart.length; i++) {
var val = shoppingCart[i];
$("#id" + val.substring(4)).val( "5");
}

Categories