Reputation: 16934
Overview
I am working on building a Kynetx ruleset that will find a bunch of Facebook ids that are on the page and then use the Kynetx Facebook module to get the Facebook avatar associated with that Facebook id. I have the JS that creates an array of Facebook ids on the page and I can process an array in KRL to retrieve Facebook avatars. What I don't have is how to get an array from the client side to the server side in KRL.
How can I get the array from the client side to the server side of KRL?
Upvotes: 1
Views: 195
Reputation: 16934
You can also do arrays of hashes if you JSON.stringify the array of hashes.
Example app:
ruleset a60x449 {
meta {
name "pass-hash-in-web-event-test"
description <<
pass-hash-in-web-event-test
>>
author "Mike Grace"
logging on
}
rule start_this_party {
select when pageview ".*"
{
notify("Now running","Building arrays to send to KNS") with sticky = true;
emit <|
var data = {};
data.userData = JSON.stringify(
[
{"name":"MikeGrace","id":234232344},
{"name":"TelegramSam","id":234089790234},
{"name":"Alex","id":2300234234234}
]
);
app = KOBJ.get_application("a60x449");
app.raise_event("process_me_data", data);
|>;
}
}
rule process_arrays_of_data {
select when web process_me_data
foreach event:param("userData").decode() setting (user)
pre {
userName = user.pick("$.name");
userId = user.pick("$.id");
output =<<
<p>
userName: #{userName}<br/>
userId: #{userId}<br/>
</p>
>>;
}
{
append("body", output);
}
}
}
Results of running on example.com
Upvotes: 1
Reputation: 16934
You can take a JavaScript array and convert it into a string and it will work if you decode it on the server side of KRL.
Example app code => https://gist.github.com/722536
Example app bookmarklet => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/send-array-to-kns-dev-bookmarklet.html
ruleset a60x442 {
meta {
name "array-passing-test"
description <<
array-passing-test
>>
author "Mike Grace"
logging on
}
rule start_your_engines {
select when pageview ".*"
{
notify("Running","...sending array to KNS") with sticky = true;
emit <|
app = KOBJ.get_application("a60x442");
var numbers = [1,2,3,4,5];
nums = JSON.stringify(numbers);
app.raise_event("process_array", {"numbers":nums});
$K("div.KOBJ_message").append("<br/>"+nums);
|>;
}
}
rule process_array {
select when web process_array
foreach event:param("numbers").decode() setting (number)
{
notify("number",number) with sticky = true;
}
}
}
Results of running app from bookmarklet on http://example.com/
Upvotes: 2
Reputation: 16934
Unfortunately, the KRL JS runtime doesn't yet support sending arrays to the server side. There is a way to accomplish what you are wanting to do though.
I built an example app that runs on this page with a bookmarklet that gets the tags that the question is tagged with and sends them to the server to be processed and then they come back.
Example app code => https://gist.github.com/707561
Example app bookmarklet => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/client-side-array-to-server-bookmarklet.html
Step by step explination of code example
Results of running app from bookmarklet:
Upvotes: 1