Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Last active August 29, 2015 14:02
Show Gist options
  • Save Widdershin/4b208f8a3de98b4dcbb1 to your computer and use it in GitHub Desktop.
Save Widdershin/4b208f8a3de98b4dcbb1 to your computer and use it in GitHub Desktop.
EDA Portfolio Challenge 6 - JS Refactor
$(document).ready(function () {
DiceController = {
dice: [],
$diceContainer: $('.dice'),
addDie: function () {
die = new Die ();
this.dice.push(die);
this.$diceContainer.append(die.rootNode());
},
rollDice: function () {
$.map(this.dice, function (die) { die.roll() });
}
}
$('.add').on('click', $.proxy(DiceController.addDie, DiceController));
$('.roll').on('click', $.proxy(DiceController.rollDice, DiceController));
function Die () {
this.view = new DieView();
this.updateFace(0);
};
Die.prototype.roll = function() {
roll_value = randomRangeInt(1, 6);
this.view.updateFace(roll_value);
};
Die.prototype.rootNode = function () {
return this.view.$face;
};
function DieView () {
this.$face = $('<div class="die"></div>');
}
DieView.prototype.updateFace = function(value) {
this.$face.text(value);
};
function randomRangeInt(min, max) {
return min + Math.round(Math.random() * (max - min));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment