Skip to content

Instantly share code, notes, and snippets.

@jiggliemon
Created June 10, 2011 07:36
Show Gist options
  • Save jiggliemon/1018394 to your computer and use it in GitHub Desktop.
Save jiggliemon/1018394 to your computer and use it in GitHub Desktop.
How to handle the FB js sdk
if(window.sml){
sml.ns('sml.util');
}
;(function(ns){
var Events = ns.Events = function(options){
var _events = {}
,key;
for(key in options){
_events[key] = options[key];
}
return {
_events: _events
,addEvent: function(type, callback){
this._events[type] = this._events[type] || [];
if(typeof callback !== 'function'){
return new TypeError('The Callback needs to be a function.');
}
this._events[type].push(callback);
}
,fireEvent: function(type) {
var events = this._events[type]
,length = events.length >>> 0
,args = Array.prototype.slice.call(arguments,1);
if(events && length) {
for (var i = 0; i < length; i++) {
if (i in events) {
events[i].apply(this,args);
}
}
}
}
};
};
sml.extend(sml, new Events());
})(sml.util);
// After `FB` is loaded and ready
SML.addEvent("FB.ready",function(){
FB.login(function(response){
if(response.error) throw new Error("Woops. There's an error.");
else {
FB.api( "/me/friends", 'get',
{ access_token:response.session.access_token }
,function(){
// After dom is ready
jQuery(document).ready(function(){
/* ... do stuff with friends ... */
});
});
}
});
});
window.fbAsyncInit = function(){
SML.fireEvent("FB.ready");
};
@rehanift
Copy link

Chase,

This is great. I wonder if we should think about adding custom events to all SML JS classes.

@jiggliemon
Copy link
Author

I've been working on a boiler plate mixin. But the idea would be that we have one global "observer" so as to keep the Module/Module relationships decoupled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment