Created
June 15, 2011 19:45
-
-
Save jugglinmike/1027926 to your computer and use it in GitHub Desktop.
Using the Strategy pattern in JavaScript
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
$.each( buttons, function( propertyName, button ) { | |
$('<button>', { | |
html: button.label, | |
id: propertyName | |
}) | |
.bind('click', button.action) | |
.appendTo( 'nav' ); | |
}); |
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
$('<button>', { | |
html: 'Login to google', | |
id: propertyName | |
}) | |
.bind('click', function() { google.accounts.user.login('https://www.google.com/m8/feeds'); | |
}) | |
.appendTo( 'nav' ); | |
$('<button>', { | |
html: 'Logout from google', | |
id: propertyName | |
}) | |
.bind('click', function(){ | |
google.accounts.user.logout(); | |
}) | |
.appendTo( 'nav' ); | |
$('<button>', { | |
html: 'Get contacts', | |
id: propertyName | |
}) | |
.bind('click', function() { | |
var contactsService = new google.gdata.contacts.ContactsService( 'Contacts Viewer' ), | |
query = new google.gdata.contacts.ContactQuery( 'https://www.google.com/m8/feeds/contacts/default/full' ); | |
query.setMaxResults( $('#numContacts').val() ); | |
contactsService.getContactFeed( | |
query, | |
function( result ) { | |
$('#contacts').remove(); | |
var $contactsHolder = $('<ul>', { | |
id: 'contacts' | |
}); | |
$.each( result.feed.entry, function( i, entry ){ | |
$.each( entry.getEmailAddresses(), function( j, address ){ | |
$contactsHolder.append( '<li>' + address.address + '</li>' ); | |
}); | |
}); | |
$contactsHolder.appendTo( 'body'); | |
}, | |
function( result ) { | |
// Log the error | |
console.log('error: ', result); | |
} | |
); | |
}) | |
.appendTo( 'nav' ); |
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
var buttons = { | |
login: { | |
label: 'Login to Google', | |
action: function() { | |
google.accounts.user.login('https://www.google.com/m8/feeds'); | |
} | |
}, | |
logout: { | |
label: 'Logout from Google', | |
action: function() { | |
google.accounts.user.logout(); | |
} | |
}, | |
getContacts: { | |
label: 'Get contacts', | |
action: function() { | |
var contactsService = new google.gdata.contacts.ContactsService( 'Contacts Viewer' ), | |
query = new google.gdata.contacts.ContactQuery( 'https://www.google.com/m8/feeds/contacts/default/full' ); | |
query.setMaxResults( $('#numContacts').val() ); | |
contactsService.getContactFeed( | |
query, | |
function( result ) { | |
$('#contacts').remove(); | |
var $contactsHolder = $('<ul>', { | |
id: 'contacts' | |
}); | |
$.each( result.feed.entry, function( i, entry ){ | |
$.each( entry.getEmailAddresses(), function( j, address ){ | |
$contactsHolder.append( '<li>' + address.address + '</li>' ); | |
}); | |
}); | |
$contactsHolder.appendTo( 'body'); | |
}, | |
function( result ) { | |
// Log the error | |
console.log('error: ', result); | |
} | |
); | |
} | |
} | |
}; |
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
var buttons = { | |
login: new Button({ | |
label: 'Login to Google', | |
action: function() { | |
google.accounts.user.login('https://www.google.com/m8/feeds'); | |
} | |
}), | |
logout: new Button({ | |
label: 'Logout from Google', | |
action: function() { | |
google.accounts.user.logout(); | |
} | |
}), | |
getContacts: new Button({ | |
label: 'Get contacts', | |
action: function() { | |
var contactsService = new google.gdata.contacts.ContactsService( 'Contacts Viewer' ), | |
query = new google.gdata.contacts.ContactQuery( 'https://www.google.com/m8/feeds/contacts/default/full' ); | |
query.setMaxResults( $('#numContacts').val() ); | |
contactsService.getContactFeed( | |
query, | |
function( result ) { | |
$('#contacts').remove(); | |
var $contactsHolder = $('<ul>', { | |
id: 'contacts' | |
}); | |
$.each( result.feed.entry, function( i, entry ){ | |
$.each( entry.getEmailAddresses(), function( j, address ){ | |
$contactsHolder.append( '<li>' + address.address + '</li>' ); | |
}); | |
}); | |
$contactsHolder.appendTo( 'body'); | |
}, | |
function( result ) { | |
// Log the error | |
console.log('error: ', result); | |
} | |
); | |
} | |
}) | |
}; |
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
var Button = function(opts) { | |
for( var attr in opts ) { | |
if(opts.hasOwnProperty(attr)) { | |
this[attr] = opts[attr]; | |
} | |
} | |
}; | |
Button.prototype.label = 'button'; | |
Button.prototype.action = function() {}; |
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
var waveImplementations = { | |
discrete: new Wave({ | |
node: context.createBufferSource(), | |
is_initialized: false, | |
init: function() { /* ... */ }, | |
readData: function( channel, callback ) { /* ... */ }, | |
connect: function( target ) { /* ... */ }, | |
disconnect: function() { /* ... */ } | |
}), | |
continuous: new Wave({ | |
node: context.createJavaScriptNode( waveForm.bufferSize, 0, 1 ), | |
is_initialized: false, | |
callback: noop, | |
init: function() { /* ... */ }, | |
readData: function( channel, callback ) { /* ... */ }, | |
connect: function( target ) { /* ... */ }, | |
disconnect: function() { /* ... */ } | |
}) | |
}, | |
wave = waveImplementations.discrete; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment