Skip to content

Instantly share code, notes, and snippets.

@davismj
Last active November 30, 2017 07:37
Show Gist options
  • Save davismj/56155adb6abcb9cb8ef64f3e3138eb78 to your computer and use it in GitHub Desktop.
Save davismj/56155adb6abcb9cb8ef64f3e3138eb78 to your computer and use it in GitHub Desktop.
Test: Automatic Number Conversion
<template>
<style>
input { display: block }
pre { max-height: 50vh; overflow-y: auto; }
</style>
<require from="my-input-custom-element"></require>
<require from="number-input-custom-element"></require>
<require from="type-custom-attribute"></require>
<div>type="number": <input type="number" value.bind="v1" /></div>
<div>type="email" <input type="email" value.bind="v2" /></div>
<div>number-input (opt in)<number-input type="number" value.bind="v4"><number-input></div>
<div>my-input (opt out)<my-input type="number" value.bind="v3"></my-input></div>
<pre scroll-down>${log}</pre>
</template>
import { observable, DOM, inject } from 'aurelia-framework';
export class App {
_log = ['Log Initialized'];
@observable v1 = 1;
@observable v2 = 1;
@observable v3 = 1;
@observable v4 = 1;
valueChanged(property, newVal) {
this._log.push(`${property} updated: ${newVal} (${typeof(newVal)})`);
}
v1Changed = (v) => this.valueChanged('type="number"', v);
v2Changed = (v) => this.valueChanged('type="email"', v);
v3Changed = (v) => this.valueChanged('my-input (opt out)', v);
v4Changed = (v) => this.valueChanged('number-input (opt in)', v);
get log() {
return this._log.join('\n');
}
}
@inject(DOM.Element)
export class ScrollDownCustomAttribute {
constructor(element) {
let scrollHeight;
const scrollDownRecursive = () => {
if (element.scrollHeight !== scrollHeight) {
element.scrollTop = element.scrollHeight;
}
scrollHeight = element.scrollHeight;
requestAnimationFrame(scrollDownRecursive)
}
scrollDownRecursive();
}
}
<!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://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
import { inlineView, bindingMode, bindable } from 'aurelia-framework';
@inlineView('<template><input type="number" value.bind="value"></template>')
export class MyInputCustomElement {
@bindable({ defaultBindingMode: bindingMode.twoWay }) value;
}
import { inlineView, bindingMode, bindable } from 'aurelia-framework';
@inlineView('<template><require from="type-custom-attribute"></require><input type="number" value.bind="value"></template>')
export class NumberInputCustomElement {
@bindable({ defaultBindingMode: bindingMode.twoWay }) value;
}
import { inject, DOM } from 'aurelia-framework';
@inject(DOM.Element)
export class TypeCustomAttribute {
constructor(element) {
this.element = element;
}
created(owningView) {
this.owningView = owningView;
}
bind() {
if (this.isNumberElement(this.element)) {
let valueBinding = this.valueBinding = this.owningView.bindings
.find(b => b.target === this.element && b.targetProperty === 'value');
if (valueBinding) {
this.updateSource = valueBinding.updateSource;
this.updateTarget = valueBinding.updateTarget;
let updateSource = this.updateSource.bind(valueBinding);
let updateTarget = this.updateTarget.bind(valueBinding);
valueBinding.updateSource = (v) => ((v = parseFloat(v)) || v === 0) && updateSource(v);
valueBinding.updateTarget = (v) => Number.isFinite(v) && updateTarget(v);
}
}
}
unbind() {
if (this.valueBinding) {
this.valueBinding.updateSource = this.updateSource;
this.valueBinding.updateTarget = this.updateTarget;
}
}
isNumberElement(element) {
return /^input$/i.test(element.tagName) && /^number$/i.test(element.type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment