Last active
April 10, 2017 09:31
-
-
Save jdanyow/744ae2416278d35b5a7d450f3cc118f5 to your computer and use it in GitHub Desktop.
removeAttribute binding behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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