Fun with JavaScript... I don't recommend this in your code! :-)

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:
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 entry

This 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 | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA