Jacques Bourque
Jacques Bourque

Reputation: 1107

Aurelia + Typescript + @bindable

I started to learn Aurelia and I am following the Getting Started guide on their website but instead of Javascript, I am using Typescript. All is good but I am having an issue making the @bindable decorator work in the nav-bar component.

My setup is as follow:

Here the content of my nav-bar.ts file:

import {bindable} from "aurelia-framework";
import {Router} from "aurelia-router";

export class NavBar {
      @bindable router: Router = null;
}

I have a tsconfig.json file as follow:

{
   "compilerOptions": {
      "noImplicitAny": false,
      "noEmitOnError": false,
      "removeComments": false,
      "target": "es5",
      "module": "system",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true
   }
}

If I let VS transpile the Typescript files, the generated nav-bar.js file looks like this:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};System.register(['aurelia-framework', 'aurelia-router'], function(exports_1) {
    var aurelia_framework_1, aurelia_router_1;
    var NavBar;
    return {
        setters:[
            function (_aurelia_framework_1) {
                aurelia_framework_1 = _aurelia_framework_1;
            },
            function (_aurelia_router_1) {
                aurelia_router_1 = _aurelia_router_1;
            }],
        execute: function() {
            NavBar = (function () {
                function NavBar() {
                    this.router = null;
                }
                __decorate([
                    aurelia_framework_1.bindable, 
                    __metadata('design:type', aurelia_router_1.Router)
                ], NavBar.prototype, "router");
                return NavBar;
            })();
            exports_1("NavBar", NavBar);
        }
    }
});

I also tried using gulp-typescript and the result is the same. With those two solution, the binding is not working and the navbar is empty.

If I use gulp-babel instead, the generated nav-bar.js file looks like this:

System.register(["aurelia-framework", "aurelia-router"], function (_export) {
    "use strict";

    var bindable, Router, NavBar;

    var _createDecoratedClass = (function () { function defineProperties(target, descriptors, initializers) { for (var i = 0; i < descriptors.length; i++) { var descriptor = descriptors[i]; var decorators = descriptor.decorators; var key = descriptor.key; delete descriptor.key; delete descriptor.decorators; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor || descriptor.initializer) descriptor.writable = true; if (decorators) { for (var f = 0; f < decorators.length; f++) { var decorator = decorators[f]; if (typeof decorator === "function") { descriptor = decorator(target, key, descriptor) || descriptor; } else { throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator); } } if (descriptor.initializer !== undefined) { initializers[key] = descriptor; continue; } } Object.defineProperty(target, key, descriptor); } } return function (Constructor, protoProps, staticProps, protoInitializers, staticInitializers) { if (protoProps) defineProperties(Constructor.prototype, protoProps, protoInitializers); if (staticProps) defineProperties(Constructor, staticProps, staticInitializers); return Constructor; }; })();

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    function _defineDecoratedPropertyDescriptor(target, key, descriptors) { var _descriptor = descriptors[key]; if (!_descriptor) return; var descriptor = {}; for (var _key in _descriptor) descriptor[_key] = _descriptor[_key]; descriptor.value = descriptor.initializer ? descriptor.initializer.call(target) : undefined; Object.defineProperty(target, key, descriptor); }

    return {
        setters: [function (_aureliaFramework) {
            bindable = _aureliaFramework.bindable;
        }, function (_aureliaRouter) {
            Router = _aureliaRouter.Router;
        }],
        execute: function () {
            NavBar = (function () {
                var _instanceInitializers = {};

                function NavBar() {
                    _classCallCheck(this, NavBar);

                    _defineDecoratedPropertyDescriptor(this, "router", _instanceInitializers);
                }

                _createDecoratedClass(NavBar, [{
                    key: "router",
                    decorators: [bindable],
                    initializer: function initializer() {
                        return null;
                    },
                    enumerable: true
                }], null, _instanceInitializers);

                return NavBar;
            })();

            _export("NavBar", NavBar);
        }
    };
});

Using the gulp-babel method works but I guess it is not necessarily the right option since I do not think babel is meant to transpile Typescript so I might have issues later on when writing some advanced code.

Questions

Is there anything I can to make it work using the official Typescript transpiler?

Anyone succeeded?

Upvotes: 2

Views: 1186

Answers (1)

Mike Graham
Mike Graham

Reputation: 1044

please try module: "amd" instead of module: "system"

Upvotes: 7

Related Questions