Skip to content

Instantly share code, notes, and snippets.

@waneka
Last active December 28, 2015 19:59
Show Gist options
  • Save waneka/7553883 to your computer and use it in GitHub Desktop.
Save waneka/7553883 to your computer and use it in GitHub Desktop.
These snippets are some examples of some javascript that I've written.
Here are two snippets of JavaScript that I am proud of from a project called queued.fm which is a web app
that allows for collaborative DJing at a party. Check the app out here: http://qd-fm.herokuapp.com/
This first snippet handles the upvote process when users click on a song in the queue. We used Firebase to handle
all the backend for the site. The Sync functions you see are just checking Firebase to see if that user has
previously voted on that particular song.
upVote: function(e){
var songItem = $(e.target).closest('li')
var songID = songItem.data('songkey')
if (Sync.checkIfUserVoted(songID) && songItem.data('songkey') != null) {
Sync.storeUserVote(songID)
var newVoteCount = (parseInt(songItem.find('.queue-vote-count').html()) + 1)
Sync.firebaseServer.child(songID).child('voteCount').set(newVoteCount)
}
},
The two Sync functions:
storeUserVote: function(songkey){
var songRef = new Firebase(this.partyAddress + songkey + '/votes/' + User.key)
songRef.set(1)
},
checkIfUserVoted: function(songkey){
var voteRef = new Firebase(this.partyAddress + songkey + '/votes/' + User.key)
var hasVoted = false
voteRef.once('value', function(snapshot){
if (snapshot.val() == null) hasVoted = true
})
return hasVoted
}
This snippet handles the sorting that takes place after a user has placed an upvote.
sortByVote: function(){
var rows = this.list.find('li')
rows.sort(function(a,b){
return (parseInt($(b).find('.queue-vote-count').text())) > (parseInt($(a).find('.queue-vote-count').text())) ? 1 : -1
})
$.each(rows, function(idx, itm){ Queue.list.append(itm) })
},
This jquery snippet handles the parallax effect on my personal website. Assigning the data-speed attribute
in the html allows me to use the same jquery code to move different html objects at different speeds.
http://tylerwaneka.com/
$window = $(window);
$('section[data-type="background"]').each(function(){
var $bgobj = $(this);
$(window).scroll(function() {
var yPos = -($window.scrollTop() / $bgobj.data('speed'));
var coords = '50% '+ yPos + 'px';
$bgobj.css({ backgroundPosition: coords });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment