Created
June 10, 2011 07:36
-
-
Save jiggliemon/1018394 to your computer and use it in GitHub Desktop.
How to handle the FB js sdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
}; |
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
Chase,
This is great. I wonder if we should think about adding custom events to all SML JS classes.