Skip to content

Instantly share code, notes, and snippets.

@plwalters
Created June 12, 2017 22:51
Show Gist options
  • Save plwalters/786dd8e457817bb02cb3b420d9d17876 to your computer and use it in GitHub Desktop.
Save plwalters/786dd8e457817bb02cb3b420d9d17876 to your computer and use it in GitHub Desktop.
DI inheritance
<template>
<require from="./two-decimal-value-converter"></require>
<h1>${message}</h1>
<input value.bind="testing | twoDecimal "/>
</template>
export class App { message = 'Hello World'; }
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
export class TwoDecimalValueConverter {
toView(value) {
if(typeof value === 'string') {
if(!value) return value;
let numValue = Number(value);
if(isNaN(numValue)) return value;
value = numValue;
}
if(typeof value === 'number') {
return value.toFixed(2);
}
return value;
}
fromView(value) {
if (typeof value !== 'string') {
return value;
}
var num = Number(value);
if (isNaN(num)) {
return null;
}
if (value.indexOf('.') > -1) {
return num;
}
if (num >= 100) num /= 100;
return num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment