Fun with JavaScript... I don't recommend this in your code! :-)
Posted by valqk on
Facebook, FB.Connect - write nice js code and reuse code call...
I'm writing a poc code that calls some FB.Connect methods.
As a quick and nasty code reuse I've come up with this code:
A method that inits and makes the actual code:
Here comes the tricky part. I wanted to be able to call multiline variable with comments in it - a normal js code but encapsulated in somethind...
If you don't know in JS you can't have multiline variable, and if you have something like:
If you replace the new lines with ' ' you'll get the whole code after a comment - commented exept you don't use / /
The solution is this:
This way you can write up your code inside the c 'function' variable and call it after that.
It's a bit tricky while you get it how it works but the code looks more readable after that.
I'm writing a poc code that calls some FB.Connect methods.
As a quick and nasty code reuse I've come up with this code:
A method that inits and makes the actual code:
function fbCall(code) { FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init('ApiKey', '/xd_receiver.htm', null); FB.ensureInit(function () { eval(code); }); }); }so far so good - it all seems ok.
Here comes the tricky part. I wanted to be able to call multiline variable with comments in it - a normal js code but encapsulated in somethind...
If you don't know in JS you can't have multiline variable, and if you have something like:
var mycall = 'FB.Connect.showFeedDialog( \'249955020144'\, //here we put some data... comment_data, '', "Awesome", null, FB.RequireConnect.promptConnect, function(){alert("Callback");}, fortune, user_message);';you'll get error while parsing because of the new lines.
If you replace the new lines with ' ' you'll get the whole code after a comment - commented exept you don't use / /
The solution is this:
function fbCall(code) { FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init('ApiKey', '/xd_receiver.htm', null); FB.ensureInit(function () { code(); }); }); } function askPerms() { var c = function() { "FB.Connect.showPermissionDialog('perms');"; } fbCall(c); }Notice the difference between two fbCall functions - the second one calls code as a function - it do not evals it.
This way you can write up your code inside the c 'function' variable and call it after that.
It's a bit tricky while you get it how it works but the code looks more readable after that.
Trackbacks
Trackback specific URI for this entryThis link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.
No Trackbacks
Comments
Display comments as Linear | ThreadedNo comments