Created
July 26, 2013 08:15
-
-
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.
This file contains hidden or 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
@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