Pippo's blog

it is all about software development

Facebook SDK Wrapper

Recently I had to use Facebook JS SDK on a site I am working on. The API is kind of nice, you can do a lot of stuff with it having the benefit to control everything on your javascript code.

The problem I went through began when I needed to run more actions after some api call and since everything is based on callbacks I could see the callback hell pattern coming.

I am using AngularJS a lot these days, some weeks back I was reading the $http object documentation and how it is built with the promise/defer solution. Turned out that I wanted to apply that to my Facebook code, I didn’t want to depend on many other libraries (nhé, it depends on underscorejs anyways…) so I wrote my own defer/promise implementation. Of course I read some references before writing it and I recommend that you make use of some proved working frameworks, like Q.

Well, it was nice to challenge myself to write the wrapper and you can use it by your own risk: Facebook Wrapper

… you can simply instatiate the object to see the available methods (you need to load FB SDK before doing so):

1
var fb = new FacebookWrapper(window.FB);

… you can also use my Defer/Promise implementation if you want, it works sort of like this:

1
2
3
4
5
6
7
8
9
10
function wrapper() {
  var q = new Queue();
  function_that_handles_a_callback('something', function(data) { q.resolve(data) });
  return q.promise;
}
function doSomethingElseAfter(response) {
  // something else with the response/data 
}

wrapper().success(doSomethingElseAfter);

Enjoy!

Comments