Showing posts with label facebook. Show all posts
Showing posts with label facebook. Show all posts

Saturday, July 18, 2009

FB Series: Integrating JS


Where are we coming from


Javascript one of the most popular language for web programming. Although it has been around for decades, - coming from Netscape 1995 - it was not until the advent of the Ajax and Web 2.0 when JavaScript came to the spotlight and brought more professional programming attention.

The OpenSource movement of JavaScript frameworks started in 2005 with prototype and script.aculo.us. Since then programming appealing JavaScript based Websites has become so much easier. Nowadays its litteraly possible to mash-up widgets with little to no JavaScript knowledge and set up a stunning page. I would however still recommend for someone not knowing JavaScript first to get the basics and maybe have some JS Reference & DOM Reference handy.

Looking at the core functionality of todays frameworks what do we have?
  • DOM Traversal with CSS selectors to locate elements
  • DOM Modification: Create/remove/modify elements
  • Short syntax for adding/removing Events
  • Short syntax for Ajax Request
  • Animations (hide/show/toggle) & Transition
  • User Interface Widgets (Drag & drop, Tree, Grid, Datepicker, Model Dialog, Menu / Toolbar, Slider, Tabbed Pane)
  • Wide Browser Support

Choosing a JS Framework

On the net you will find a mirrad of comparisons of popular frameworks. The most popular OpenSource and free JS Frameworks available today are:

* Prototype (&Scriptaculous for UI)
* Dojo
* jQuery
* YUI
* Mootools

I would say that all of those have their advantages and drawbacks and there's not really an obvious winner. It really depends on what you use it for. Some helpful links that helped me choosing are:

Wikipedia - to get a feeling on the features
unbiased functional comparison
Stackoverflow comparing JQuery Dojo and more
Performance comparison

Choosing a framework, as mentioned, depends a bit on how you want to use JavaScript:


Plug-and-Play:
  • Drop in a “calendar widget” or “tabbed navigation”
  • Little, to no, JavaScript experience required.
  • Just customize some options and go.
  • No flexibility.
=> Widgets

Some Assembly Required
  • Write common utilities
  • Click a link, load a page via Ajax
  • Build a dynamic menu
  • Creating interactive forms
  • Use pre-made code to distance yourself from browser bugs.
  • Flexible, until you hit a browser bug.
=> Libraries

Down-and-Dirty
  • Write all JavaScript code from scratch
  • Deal, directly, with browser bugs
  • Quirksmode is your lifeline
  • Excessively flexible, to the point of hinderance.
=> Raw Java Script

Of course you can also mix different approaches, as you can also mix Frameworks but of course in terms of maintainability and productivity it would better if you don't have to.

For my personal use case I had the following requirements:
  1. Easy to learn with minimal intuitive syntax
  2. Lightweight solution
  3. Availability of some widgets: Datepicker, Grid, maybe more
  4. Appealing Web 2.0 effects
  5. Production quality
Right now I have choosen jQuery (incl. jQuery UI) and see how far I get with it. It was to me the most appealing in terms of the above criterias. I am not sure if I need a sophisticated data table, if so I might have another look at YUI data table which seems to me one of the best widgets for this (compare to e.g. this jQuery plugin).

For development i guess its essential to still have a good Javascript support in your IDE and a JavaScript debugger - I use Firebug and Netbeans for this. If you are into Eclipse I strongly suggest to take a look at Aptana, their IDE is really great for JavaScript and PHP, but unfortunately not optimal for Facebook development.

As a last hint: consider using Google Ajax Libs for speed up JS loading on your clients. However do not use it in local development. It just does not work reliable (loads too late etc.)

Friday, July 10, 2009

FB Series: Locally debug your PHP Facebook App

Starting with Facebook applications is a rather a bumpy road. First there is the scattered and often not up-to-date documentation on the Facebook wiki. Its a pain to go through. But ok once you managed to get the important bits its ok.

What I am talking about is the testing. Its quite astonishing that this is not thought through at all: Facebook does not offer a Test environment (!). So this means developers have to test their apps in the real Facebook which means. Developers have to create 2 apps one for test one for prod with different configurations canvas urls etc. as well as create Testusers - or they want to annoy their friends with test apps.

If your like me you prefer debugging to changelogginguploadtestchangeuploadtest cycles. With Facebook Apps this was not straigthforward so therefore here is a easy end-to-end explanation for you how to setup your php facebook app for local debugging - which i didnt find on the net.

Prerequisistes: You have apache and php 5.x installed.

  1. Set up your computer for portforwarding. Portworward.com has good explanations for all different kind of configurations and routers.
  2. Get yourself a alias from dyndns.org, something like myfacebookapp.dyndns.org pointing to your ip. In order to update your dynamic ip if you have one, install the dyndns updater.
  3. Try accessing your apache file via your dyndyns alias(myfacebookapp.dyndns.org). Keep in mind that many routers do not support ip loopback, therefore you might need to go through a proxy to do that. Also make sure to configure your firewall to open the ports your forwarding.
  4. If this all worked you are ready to setup your php environment. Startup (or install) your php ide of choice with integrated xdebugging for php (like Netbeans 6.7 or Aptana 1.5)
  5. You need to configure your php.ini file to support the xdebug. At the end of your php.ini file add:

    zend_extension_ts="c:/php/ext/php_xdebug-2.0.5-5.2.dll"
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_mode=req
    xdebug.remote_host=localhost
    xdebug.remote_port=9000

    As you see I use windows change your path to the xdebug .dll / .so as you need. You might need to download it from xdebug.org


  6. Now you should be ready to startup a debug session of a file locally. Test it
  7. In order to debug HTTP Sessions its important that you have the parameter XDEBUG_SESSION_START= (for Netbeans its XDEBUG_SESSION_START=netbeans-xdebug) in your request url. So this gets a bit tricky for facebook apps since facebook is calling the canvas url I think that cannot contain query params (I guess ..) . Anyway no need to try or think either there is a nice firefox plugin that does that for us.

That's it: Fire up a debugging session in your ide, add a breakpoint in your facebook app and try access your facebook app from facebook. You should end up in your local debugging session.