DamianEdwards / grunt-tsng

A TypeScript pre-processor for AngularJS
Apache License 2.0
29 stars 3 forks source link

Class definitions for config/run/filters + missing annotations #6

Open rochdev opened 10 years ago

rochdev commented 10 years ago

Right now controllers, directives and services are using classes, but config/run and filters use functions exclusively. I think it would feel more natural in TypeScript to also support class definitions for those.

For example:

MyApp/Configuration.ts

module MyApp {
  //@NgConfig
  class Configuration {
    constructor($routeProvider: ng.IRouteProvider) {
      // Configure routes here
    }
  }
}

MyApp/Startup.ts

module MyApp {
  //@NgRun
  class Startup {
    constructor($translate: ng.translate.ITranslateService) {
      // Set translation at runtime
    }
  }
}

MyApp/TruncateFilter.ts

module MyApp {
  //@NgFilter('truncate')
  class TruncateFilter {
    constructor(input: string, length: number) {
      if (!input) {
        return input;
      }

      if (input.length <= length) {
        return input;
      } else {
        return input.substr(0, length).trim() + "…";
      }
    }
  }
}

Also, there are a few module methods which have no annotation at the moment. Those could look something like this:

MyApp/AFactory.ts

module MyApp {
  //@NgFactory('aService')
  class AFactory {
    constructor($http: ng.IHttpService) {
      return new AService($http);
    }
  }
}

MyApp/AProvider.ts

module MyApp {
  //@NgProvider('aService')
  class AProvider implements ng.IServiceProvider {
    public $get($http: ng.IHttpService) {
      return new AService($http);
    }
  }
}

MyApp/MyApp.ts

module MyApp {
  //@NgValue('myFoo')
  var foo = 'some string';

  //@NgConstant('myBar')
  var bar = [1, 2, 3];
}

MyApp/SomeAnimationFactory.ts

module MyApp {
  //@NgAnimation('.animation-name')
  class SomeAnimationFactory {
    constructor($inject1: any, $inject2: any) {
      return new SomeAnimation($inject1, $inject2);
    }
  }
}

Some of these may be wrong I am still new to TypeScript but bear with me ^^

Let me know what you think!

edit: updated title and added factory/provider/value/constant/animation