PDA

View Full Version : Trouble passing boolean to plugin



townivan
07-09-2006, 08:57 AM
Has anyone else run into this?

I want to pass a boolean to my plugin so I do something like:

var variables:Object = new Object();
variables.b1 = myBoolean;
es.pluginRequest("myPlugin", "myMethod", variables);
and in the plugin I receive it like:

function pluginRequest(hash) {
var method = hash.get("Method");
var caller = hash.get("ExecutingUserName");
if (method == "myMethod") {
b1 = hash.get("b1");
}

b1 is not a boolean. When I run:

trace("b1 type = " + typeof b1);

It's an object. :confused:

I can't use:

b1 = Boolean(hash.get("b1"));
because that will always translate into true;

jobem
07-27-2006, 02:25 AM
Hi Ivan,

When Flash clients send variables to a plugin they always arrive as strings. So a boolean is converted to a string, anda number to a string, etc.

Within a plugin I usually create a little helper function to do the conversion for me. Such as:



function stringToBoolean(str) {
return str.toLowerCase() == "true" ? true : false;
}

Then your plugin could do this:

function pluginRequest(hash) {
var method = hash.get("Method");
var caller = hash.get("ExecutingUserName");
if (method == "myMethod") {
b1 = stringToBoolean(hash.get("b1"));
}
}

Hope this helps

townivan
07-28-2006, 03:10 AM
Thanks Jobe! Very helpful. :)