groovy pass map to javascript from controller - javascript

I am fairly new to Grails MVC and javascript.
I am encountering a problem wherein i want to pass a map object from the controller to a javascript function.
Currently two parameters are passed to the javascript function which are comma seperated and this works fine
eg. someControllerFunction() {
variableLink = "j-javaScriptFunction-${stringArgs1},${stringArgs2}" // This is a link for an ajax call
}
The javascript function structure looks like this
function someJavaScriptFunction (details) {
var d = details.split(",");
var strintArgs1 = d[0];
var stringArgs2 = d[1];
ajax":{
"url":"${request.contextPath}/controller/methodInController?strintArgs1=" + strintArgs1 + "&stringArgs2=" +stringArgs2
},
}
The Controller function which is called in the ajax currently looks like this
methodInController (String strintArgs1,String strintArgs2){
//some operation
}
Now i want to pass a map object from the controller function to the javascript function but i am not able to as the javascript considers the map as an invalid String object.
Below are the changes i have made to the three functions but i am getting an error during the ajax call saying "Uncaught SyntaxError: Unexpected string"
eg. someControllerFunction() {
variableLink = "j-javaScriptFunction-${stringArgs1},${stringArgs2},${mapArg}" // This is a link for an ajax call
}
The map object looks like this
mapArg = [a:[],b:[],c:[],d:[]]
The javascript function structure looks like this
function someJavaScriptFunction (details) {
var d = details.split(",");
var strintArgs1 = d[0];
var stringArgs2 = d[1];
var mapArg = d[2];
ajax":{
"url":"${request.contextPath}/controller/methodInController?strintArgs1=" + strintArgs1 + "&stringArgs2=" +stringArgs2 + "&mapArg=" +mapArg
},
}
The Controller function which is called in the ajax currently looks like this
methodInController (String strintArgs1, String strintArgs2, Object mapArg){
//some operation
}
It might be something to do with the way i am passing it to the javascript function but i am not able to figure out the exact reason.
Could anyone please help me to understand what am i doing wrong.
Thanks in advance

It looks like you might have to cast the map to JSON first (which would explain why its a string). See this answer: https://stackoverflow.com/a/2064341/1902587

Related

Passing data to a function as a key and value

I have been trying to execute a function as follows, but am not succeeding.
var leagueSelect = "allLeagues";
loadTable("#loadLeaguesTable","php/leagueTable.php",'leagueSelect',leagueSelect);
The loadTable function:
function loadTable(tableDiv, tableURL, tableType, tableVal){
$.post(tableURL,{tableType:tableVal},function(data){
$(tableDiv).append(data);
});
}
I'm trying to send the values in the post in this format: { leagueSelect: leagueSelect }.
If I hard-code leagueSelect into my function in place of the tableType parameter in the $.post function, it works.
How am I supposed to send 'leagueSelect' properly in my function call?
I'm thinking the data[key] = value method but I didn't get that working either.
Thanks.
When you assign the variable you're actually creating a new object with the property tableType set to whatever the tableVal value is.
Instead you need to use the object[key] = val; syntax or pass the post data in directly like this:
var leagueSelect = "allLeagues";
loadTable("#loadLeaguesTable", "php/leagueTable.php", { leagueSelect: leagueSelect });
function loadTable(tableDiv, tableURL, postData) {
$.post(tableURL, postData, function(data) {
$(tableDiv).append(data);
});
}

how can I parse this JSON into several 'objects'?

I have an object defined like this:
function Question_Value__c () {
this.Id=null;
this.Name=null;
this.Question__c=null;
this.Order__c=null;
}
I'm trying to deserialize this JSON into several of these objects but I can't get it to work:
[{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"}]
Here's what I have so far:
allValues = new Array(new Question_Value__c());
$(returnedJSON).each(function() {
allValues.push($(this));
console.log(allValues[0].id);
});
Any assistance is much appreciated, I'm pretty new to working with JSON and javascript.
First of all, you need to put actual " in your JSON string, it is not valid to encode these caracters using ", that would be in HTML however.
In addition, if you want to populate your Question_Value_c objects with data, you should change your constructor function to something like:
function Question_Value__c(data) {
$.extend(this, data); //have a look at $.extend
}
Then you could do that (note that your objects do not have Question__c and Order__c properties):
var json = '[{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"},{"id":"a0Dd000000RTVsAEAX","name":"ee"}]',
allValues = [];
$.each($.parseJSON(json), (function () {
allValues.push(new Question_Value__c(this));
}));
console.log(allValues);

How to create javascript function lookup object?

I'm attempting to create a function lookup in Javascript essentially mapping a data type to a function that does something for that data type. Right now I have something similar to:
var Namespace = Namespace || {};
Namespace.MyObj = function () {
var stringFunc = function(someData) {
//Do some string stuff with someData
};
var intFunc = function(someData) {
//Do some int stuff with someData
};
var myLookUp = {
'string': stringFunc,
'int' : intFunc
};
return {
PublicMethod: function (dataType, someData) {
myLookUp[dataType](someData);
}
};
} ();
When I invoke Namespace.MyObj.PublicMethod(dataType, someData) I get an error that myLookUp is not defined. I'm assuming I'm not going about setting up the function lookup object correctly, but not sure how to do so. Thanks for any help.
The problem might simply be incorrect case
myLookup[dataType](someData);
should be (notice the capital U)
myLookUp[dataType](someData);
Just looked at my post after I wrote it up, stupid oversight, I'm declaring the properties as strings, instead of just properties.
....
var myLookUp = {
string: stringFunc,
int: intFunc
};
....
Fixes the issue.
Some additional follow up, in my actual code dataType is the result of a jQuery select. Don't know why or if this would be browser dependant (I'm using FireFox), but using double quotes around the property definition works, single quotes does not, and no quotes works as well. :-\

Why does this function return as undefined?

String.prototype.parse = function(f) {
alert(this.replace(f, ""));
};
var a = "Hello World";
parse.apply(a, ["Hello"]);
Is the code correct?
No, that’s not correct. The function is defined as String.prototype.parse, so it is not available as parse (in fact, parse is undefined).
You could run it like the following:
String.prototype.parse.apply(a, ["Hello"]);
But actually, the reason why you add the function to the prototype of String is that you extend String objects with that function. So you actually should just run the function like this:
a.parse("Hello");
edit:
Oh, and in response to your question title “Why does this function return as undefined?”: The function doesn’t return anything, because you don’t tell the function to return anything. You could for example define it like this to return the replaced string (instead of alerting it):
String.prototype.parse = function(f) {
return this.replace(f, "");
};
And then you could alert the return value of the function:
alert(a.parse("Hello"));
There is no such variable parse defined in your code sample. If you really want to apply the function later on, you should do this:
// Capture function as a local variable first
var parse = function(f) { alert(this.replace(f, "")); };
String.prototype.parse = parse;

Flash CS4 pass variable from javascript function to actionscript and back

I want to pass the result of a js function into actionscript when the js function is called from actionscript. The js parses a url and returns the var in the url.
In actionscript the functions are declared:
function gup(name1:String) :void {
flash.external.ExternalInterface.call("gup", name1);
}
function printAlert(group2:String) :void {
flash.external.ExternalInterface.call("printAlert", group2);
}
then later in actions I call gup() which should return the var which I turn around and print as an alert to check what value is there. "group" is the name of the var I want to get out of the url to use for branching in the swf. If I just define whichGroup the alert works fine. trying to get the return value of the js function the alert value is null
var whichGroup = gup("group");
printAlert(whichGroup);
ActionScript
function printAlert(group2:String):void {
var retValue:String = ExternalInterface.call("printAlert", group2);
trace(retValue);
}
javascript:
function printAlert(grp) {
return "Received group " + grp;
}

Categories