Skip to content

Instantly share code, notes, and snippets.

@vanm
vanm / gist:1355978
Created November 10, 2011 19:58
Reach API Example
var _reach = _reach || [];
_reach.push({
container: 'corp_stream',
domain: 'http://socialcast.com/',
token: '12345'
});
@vanm
vanm / gist:1355968
Created November 10, 2011 19:52
API Access Point
(function(){
// Private (not accessible in global scope)
function UtilityFunction(){}
function ThirdPartyAPI(){}
// Export a single global access point the ThirdPartyAPI
window.thirdPartyAPI = window.thirdPartyAPI || new ThirdPartyAPI();
})();
@vanm
vanm / gist:1355790
Created November 10, 2011 19:06
Dynamic Script Generation
(function(){
var e = document.createElement('script'); e.type='text/javascript'; e.async = true;
e.src = document.location.protocol + '//yourdomain.com/packaged_third_party_javascript.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
})();
@vanm
vanm / gist:1029643
Created June 16, 2011 16:36
Remove Associated Elements
// Instantiate a Component
var newComponent = new Component({
// Init creates domElement and associatedElements properties
// Override removeElement method
removeElement: function(){
this.domElement.remove();
@vanm
vanm / gist:1026025
Created June 14, 2011 21:58
jQuery cleanData
// --------------------------------------------------------
// Excerpt from jQuery's cleanData() (MIT Licence)
// Embrace circular references in event bindings, but clean up.
if ( data && data.events ) {
for ( var type in data.events ) {
if ( special[ type ] ) {
jQuery.event.remove( elem, type );
} else {
jQuery.removeEvent( elem, type, data.handle );
}
@vanm
vanm / gist:1025997
Created June 14, 2011 21:48
MS Closure Examples
// MS example of a memory leak "caused by closure"
hookup(document.getElementById('menu'));
function hookup(element) {
// Reference to #menu element available within
// mouse scope via closure
function mouse (){}
// mouse attached to #menu element via event.
@vanm
vanm / gist:1025944
Created June 14, 2011 21:30
Remove associated element
// Instantiate a Component (overriding removeElement method)
var newComponent = new Component({
removeElement: function(){
this.domElement.remove();
this.trigger('removeElement');
}
});
@vanm
vanm / gist:1025841
Created June 14, 2011 20:49
Explicit null comment
// Remove the message from the Cache
// Must explicitly null out this reference for IE GC
messagesCache[messageID] = null;
delete messagesCache[messageID];