Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created November 12, 2014 14:21
Show Gist options
  • Save skatenerd/9570027e80850a43f478 to your computer and use it in GitHub Desktop.
Save skatenerd/9570027e80850a43f478 to your computer and use it in GitHub Desktop.
databinding
<!DOCTYPE html>
<html lang="en" ng-app="temperature-converter" class="no-js">
<head>
<meta charset="utf-8">
<script src="./x.jquery-1.8.3.min.js"></script>
<script src="./temperature.js"></script>
<script type="text/javascript">
var TemperatureController = function(fahrenheit_input, celsius_input) {
var controller = this;
this.fahrenheit_input = fahrenheit_input;
this.celsius_input = celsius_input;
this.sqrt_celsius = $("[name=sqrt_celsius]");
this.sqrt_fahrenheit = $("[name=sqrt_fahrenheit]");
this.freezing_indicator = $("[name=freezing_indicator]");
this.fahrenheit_input.on('input', function(){
controller.temperature_truth = new Temperature((this.value || 0), 'fahrenheit');
controller.impose_invariants();
});
this.celsius_input.on('input', function(){
controller.temperature_truth = new Temperature((this.value || 0), 'celsius');
controller.impose_invariants();
});
this.sqrt_celsius.on('click', function(){
controller.temperature_truth = new Temperature(Math.sqrt(controller.temperature_truth.in_celsius()), 'celsius');
controller.impose_invariants();
});
this.sqrt_fahrenheit.on('click', function(){
controller.temperature_truth = new Temperature(Math.sqrt(controller.temperature_truth.in_fahrenheit()), 'fahrenheit');
controller.impose_invariants();
});
}
TemperatureController.prototype.impose_invariants = function(){
this.fahrenheit_input.val(this.temperature_truth.in_fahrenheit());
this.celsius_input.val(this.temperature_truth.in_celsius());
this.sqrt_celsius.prop('disabled', this.temperature_truth.in_celsius() < 0);
this.sqrt_fahrenheit.prop('disabled', this.temperature_truth.in_fahrenheit() < 0);
if(this.temperature_truth.in_celsius() <= 0) {
this.freezing_indicator.text("FREEZING");
} else {
this.freezing_indicator.text("WARM ISH");
}
}
$(function(){new TemperatureController($("[name=fahrenheit]"), $("[name=celsius]"));});
</script>
</head>
<body>
<label>fahrenheit:</label>
<input name="fahrenheit"/>
<label>celsius:</label>
<input name="celsius"/>
<button name="sqrt_celsius">SQRT THE CELSIUS</button>
<button name="sqrt_fahrenheit">SQRT THE FAHRENHEIT</button>
<h2 name="freezing_indicator"></button>
</body>
</html>
var Temperature = function(quantity, units) {
this.quantity = quantity;
if(!isNaN(parseFloat(this.quantity))){
this.quantity = parseFloat(this.quantity);
}
this.units = units;
}
Temperature.prototype.in_celsius = function(){
if (this.units == 'celsius'){
return this.quantity;
}
return this.fahrenheit_to_celsius(this.quantity);
}
Temperature.prototype.in_fahrenheit = function(){
if (this.units == 'fahrenheit'){
return this.quantity;
}
return this.celsius_to_fahrenheit(this.quantity);
}
////////////////////////PRIVATE/////////////////////
Temperature.prototype.celsius_to_fahrenheit = function(celsius){
return (celsius * 1.8) + 32;
}
Temperature.prototype.fahrenheit_to_celsius = function(fahrenheit){
return (fahrenheit - 32) / 1.8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment