Skip to content

Instantly share code, notes, and snippets.

@jmvtrinidad
Forked from jdanyow/app.html
Created April 10, 2017 09:31
Show Gist options
  • Save jmvtrinidad/d8d09b032b20c13e0d27651fba1e3d7b to your computer and use it in GitHub Desktop.
Save jmvtrinidad/d8d09b032b20c13e0d27651fba1e3d7b to your computer and use it in GitHub Desktop.
removeAttribute binding behavior
<template>
<require from="./remove-attribute"></require>
<input type="checkbox" ref="myInput">
<input ref="test" foo.bind="myInput.checked & removeAttribute">
<pre><code>${test.outerHTML}</code></pre>
</template>
export class App {
bind() {
// this is not required... just cleaning up the output
this.test.removeAttribute('ref');
this.test.removeAttribute('class');
this.test.removeAttribute('au-target-id');
this.test.removeAttribute('foo.bind');
}
}
<!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>
export class RemoveAttributeBindingBehavior {
bind(binding, source) {
binding.targetObserver = new RemoveAttributeObserver(binding.target, binding.targetProperty);
}
}
class RemoveAttributeObserver {
constructor(element, propertyName) {
this.element = element;
this.propertyName = propertyName;
}
getValue() {
return this.element.getAttribute(this.propertyName);
}
setValue(newValue) {
if (newValue) {
this.element.setAttribute(this.propertyName, newValue);
} else {
this.element.removeAttribute(this.propertyName);
}
}
subscribe() {
throw new Error(`Observation of a "${this.element.nodeName}" element\'s "${this.propertyName}" property is not supported.`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment