Search code examples
typescriptaurelia

Aurelia - Trouble getting CssAnimator working with simple message div


First of all I am using Typescript and this blog post by Mikhail Shilkov.

Whilst I am using Typescript this post is in Javascript and I wonder if this is the reason its not working... (see error below)

I have set up a div at the top of my page that has the variable "test" that I have linked to a button that changes 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 view:

public test: string = "pears";

testAnimation() {
    this.test = "apples";
}

As per the post I am using custom Attributes.

My AnimateOnChangeCustomAttribute class is below and its here that I am getting an error.

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');
        });
    }
}

It has an error on this line:

this.animator.addClass(this.element, 'background-animation').then(() => {

...saying that it cannot 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)

Wondering if someone has an idea why its failing here?

Extra info I used the same identical css as per the blog.

.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 added this css file to the top of the page that has the "" tags for this 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>

Solution

  • Try changing the constructor to: constructor(private element: Element, public animator: CssAnimator)