Currently, I am utilizing Typescript and referencing an informative blog post by Mikhail Shilkov.
Even though I am implementing Typescript, the blog post revolves around Javascript. This has led me to ponder whether this difference is the root cause of the issue I am encountering (refer to the error below).
At the beginning of my page, there is a div housing the variable "test" which is associated with a button responsible for altering the value of test.
<template>
<require from="./animateOnChange"></require>
<div animateonchange.bind="test">
${test}
</div>
<button click.delegate="testAnimation()" type="button">test Animation</button>
</template>
In the visual representation:
public test: string = "pears";
testAnimation() {
this.test = "apples";
}
Following the post, I have embraced custom Attributes in my implementation.
The AnimateOnChangeCustomAttribute class where the error seems to arise is presented below.
import { autoinject, customAttribute } from 'aurelia-framework';
import { CssAnimator } from 'aurelia-animator-css';
@customAttribute('animateonchange')
@autoinject
export class AnimateOnChangeCustomAttribute {
element: any;
constructor(element, public animator: CssAnimator) {
this.element = element;
}
valueChanged(newValue) {
console.log("ELEMENT, NEW VALUE: ", this.element, newValue);
this.animator.addClass(this.element, 'background-animation').then(() => {
this.animator.removeClass(this.element, 'background-animation');
});
}
}
An issue emerges at this particular line:
this.animator.addClass(this.element, 'background-animation').then(() => {
...indicating that it is unable to read the property 'contains' of undefined.
Uncaught (in promise) TypeError: Cannot read property 'contains' of undefined
at aurelia-animator-css.js:373
at new Promise (<anonymous>)
at CssAnimator.addClass (aurelia-animator-css.js:366)
at AnimateOnChangeCustomAttribute.services/messages/animateOnChange.AnimateOnChangeCustomAttribute.valueChanged (animateOnChange.ts:16)
at BehaviorPropertyObserver.selfSubscriber (aurelia-templating.js:3662)
at BehaviorPropertyObserver.call (aurelia-templating.js:3527)
at Controller.bind (aurelia-templating.js:3388)
at View.bind (aurelia-templating.js:1406)
at Controller.bind (aurelia-templating.js:3409)
at View.bind (aurelia-templating.js:1406)
If anyone possesses insights as to why this failure is occurring, please do share your thoughts.
Additional details I have utilized the exact CSS specifications outlined in the aforementioned blog post.
.background-animation-add {
-webkit-animation: changeBack 0.5s;
animation: changeBack 0.5s;
}
.background-animation-remove {
-webkit-animation: fadeIn 0.5s;
animation: fadeIn 0.5s;
}
@-webkit-keyframes changeBack {
0% { background-color: white; }
50% { background-color: lightgreen; }
100% { background-color: white; }
}
@keyframes changeBack {
0% { background-color: white; }
50% { background-color: lightgreen; }
100% { background-color: white; }
}
I have included this CSS file at the top of the page within the "" tags designated for this specific view model.
<template>
<require from="../navmenu/navmenu"></require>
<require from="../../services/messages/messages"></require>
<require from="./app.css"></require>
<!--We want the nav bar to span the page-->
<div class="container-fluid">
<navmenu router.bind="router"></navmenu>
</div>
<div class="container">
<div className='col-sm-12'>
<div className='row'>
<messages></messages> //HERE IS THE VIEW MODEL.
</div>
<!--We want the media to centre so we use just container-->
<div className='row'>
<router-view></router-view>
</div>
</div>
</div>
</template>