Skip to content

Instantly share code, notes, and snippets.

@joelip
Created July 26, 2013 08:15
Show Gist options
  • Save joelip/6087156 to your computer and use it in GitHub Desktop.
Save joelip/6087156 to your computer and use it in GitHub Desktop.
raffle.js.coffee file before editing it to deal with $resource. From initial Angularjs practice.
@RaffleCtrl = ($scope) -> # using @ before Raffle makes this visible to angularjs
# pass in scope (object used to interact with a view)
# you can set variables and functions on it
$scope.entries = [
{name: "Larry"}
{name: "Brian"}
{name: "Jennifer"}
]
$scope.addEntry = ->
$scope.entries.push($scope.newEntry)
$scope.newEntry = {}
# create a scope called entries which is an array and pushes all new items to the end
# of that array. Then resets the value after the item is pushed.
$scope.drawWinner = ->
pool = []
angular.forEach $scope.entries, (entry) ->
pool.push(entry) if !entry.winner
# loops through all entries and pushes the entry to array 'pool' if it hasn't
# won yet
if pool.length > 0
# now we only select entries from the pool, not from all entries
entry = pool[Math.floor(Math.random()*pool.length)]
# defines entry as a random entry in the entries array
# does this by multiplying a random decimal number between 0 and 1 by the
# length of the entire array and then rounding the resulting number down
entry.winner = true
$scope.lastWinner = entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment