From c7a37fabcd9884c20df5110d5c304094475fcdff Mon Sep 17 00:00:00 2001 From: Jacob Eggers Date: Tue, 7 Jan 2014 03:17:22 +0100 Subject: [PATCH] TypeScriptifying the generators. --- templates/typescript/app.ts | 8 ++++- templates/typescript/controller.ts | 27 ++++++++++++----- templates/typescript/decorator.ts | 21 +++++++++---- templates/typescript/directive.ts | 28 +++++++++++------ templates/typescript/filter.ts | 20 +++++++++---- templates/typescript/service/factory.ts | 29 ++++++++++-------- templates/typescript/service/provider.ts | 38 ++++++++++++------------ templates/typescript/service/service.ts | 16 ++++++++-- 8 files changed, 125 insertions(+), 62 deletions(-) diff --git a/templates/typescript/app.ts b/templates/typescript/app.ts index 58f75e3dc..d2641ba14 100644 --- a/templates/typescript/app.ts +++ b/templates/typescript/app.ts @@ -1,7 +1,13 @@ +/// +<% if (ngRoute) { %>/// <% } %> +/// +/// +/// + 'use strict'; angular.module('<%= scriptAppName %>', [<%= angularModules %>])<% if (ngRoute) { %> - .config(function ($routeProvider) { + .config(function ($routeProvider:ng.route.IRouteProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', diff --git a/templates/typescript/controller.ts b/templates/typescript/controller.ts index 216a5ea20..b11b4a032 100644 --- a/templates/typescript/controller.ts +++ b/templates/typescript/controller.ts @@ -1,10 +1,23 @@ +/// + 'use strict'; +module <%= scriptAppName %> { + export interface I<%= classedName %>Scope extends ng.IScope { + awesomeThings: any[]; + } + + export class <%= classedName %>Ctrl { + + constructor (private $scope:I<%= classedName %>Scope) { + $scope.awesomeThings = [ + 'HTML5 Boilerplate', + 'AngularJS', + 'Karma' + ]; + } + } +} + angular.module('<%= scriptAppName %>') - .controller('<%= classedName %>Ctrl', function ($scope) { - $scope.awesomeThings = [ - 'HTML5 Boilerplate', - 'AngularJS', - 'Karma' - ]; - }); + .controller('<%= classedName %>Ctrl', <%= scriptAppName %>.<%= classedName %>Ctrl); diff --git a/templates/typescript/decorator.ts b/templates/typescript/decorator.ts index 57efaa3f6..5433d929f 100644 --- a/templates/typescript/decorator.ts +++ b/templates/typescript/decorator.ts @@ -1,9 +1,18 @@ +/// + 'use strict'; +module <%= scriptAppName %>{ + export function <%= cameledName %>DecoratorProvider($provide:ng.auto.IProvideService):void { + //decorate <%= cameledName %> + $provide.decorator('<%= cameledName %>', <%= cameledName %>Decorator); + } + + export function <%= cameledName %>Decorator($delegate) { + // decorate the $delegate + return $delegate; + } +} + angular.module('<%= scriptAppName %>') - .config(function ($provide) { - $provide.decorator('<%= cameledName %>', function ($delegate) { - // decorate the $delegate - return $delegate; - }); - }); + .config(<%= scriptAppName %>.<%= cameledName %>DecoratorProvider); diff --git a/templates/typescript/directive.ts b/templates/typescript/directive.ts index 57b0e1f3d..e74708579 100644 --- a/templates/typescript/directive.ts +++ b/templates/typescript/directive.ts @@ -1,12 +1,22 @@ +/// + 'use strict'; +module <%= scriptAppName %> { + + export class <%= classedName %> implements ng.IDirective { + template = '
'; + restrict = 'E'; + link = function (scope, element: JQuery, attrs: ng.IAttributes):void { + element.text('this is the <%= cameledName %> directive'); + } + } + + export function <%= cameledName %>Factory() { + return new <%= scriptAppName %>.<%= classedName %>(); + } + +} + angular.module('<%= scriptAppName %>') - .directive('<%= cameledName %>', function () { - return { - template: '
', - restrict: 'E', - link: function postLink(scope, element, attrs) { - element.text('this is the <%= cameledName %> directive'); - } - }; - }); + .directive('<%= cameledName %>', <%= scriptAppName %>.<%= cameledName %>Factory); diff --git a/templates/typescript/filter.ts b/templates/typescript/filter.ts index 9f897ab76..dcd3fae33 100644 --- a/templates/typescript/filter.ts +++ b/templates/typescript/filter.ts @@ -1,8 +1,18 @@ +/// + 'use strict'; +module <%= scriptAppName %> { + export function <%= cameledName %>FilterFactory():Function { + return <%= cameledName %>Filter; + } + + function <%= cameledName %>Filter(input, param) { + //usage {{"text" | <%= cameledName %>: "suffix"}} + //returns '<%= cameledName %> filter: text suffix' + return '<%= cameledName %> filter: ' + input + (param ? ' ' + param: ''); + } +} + angular.module('<%= scriptAppName %>') - .filter('<%= cameledName %>', function () { - return function (input) { - return '<%= cameledName %> filter: ' + input; - }; - }); + .filter('<%= cameledName %>', <%= scriptAppName %>.<%= cameledName %>FilterFactory); \ No newline at end of file diff --git a/templates/typescript/service/factory.ts b/templates/typescript/service/factory.ts index f7e942eb9..25c325dca 100644 --- a/templates/typescript/service/factory.ts +++ b/templates/typescript/service/factory.ts @@ -1,16 +1,21 @@ +/// + 'use strict'; -angular.module('<%= scriptAppName %>') - .factory('<%= cameledName %>', function () { - // Service logic - // ... +module <%= scriptAppName %> { + export function <%= cameledName %>Factory() { + return new <%= classedName %>(42); + } + export class <%= classedName %> { - var meaningOfLife = 42; + constructor (private meaningOfLife) { + } - // Public API here - return { - someMethod: function () { - return meaningOfLife; - } - }; - }); + someMethod(){ + return this.meaningOfLife + } + } +} + +angular.module('<%= scriptAppName %>') + .factory('<%= cameledName %>', <%= scriptAppName %>.<%= cameledName %>Factory); diff --git a/templates/typescript/service/provider.ts b/templates/typescript/service/provider.ts index 101e774c5..a102023d9 100644 --- a/templates/typescript/service/provider.ts +++ b/templates/typescript/service/provider.ts @@ -1,25 +1,25 @@ +/// + 'use strict'; -angular.module('<%= scriptAppName %>') - .provider('<%= cameledName %>', function () { +module <%= scriptAppName %> { - // Private variables - var salutation = 'Hello'; + var salutation:string; + + export class Greeter { + greet = () => salutation; + } + + + export class <%= classedName %>Provider { + $get = () => new Greeter(); + + // Public API for configuration + setSalutation = (s:string) => salutation = s; + } - // Private constructor - function Greeter() { - this.greet = function () { - return salutation; - }; - } +} - // Public API for configuration - this.setSalutation = function (s) { - salutation = s; - }; - // Method for instantiating - this.$get = function () { - return new Greeter(); - }; - }); +angular.module('<%= scriptAppName %>') + .provider('<%= cameledName %>', <%= scriptAppName %>.<%= classedName %>Provider); diff --git a/templates/typescript/service/service.ts b/templates/typescript/service/service.ts index 1adfe44b4..13b45a5d1 100644 --- a/templates/typescript/service/service.ts +++ b/templates/typescript/service/service.ts @@ -1,6 +1,16 @@ +/// + 'use strict'; +module <%= scriptAppName %> { + export class <%= classedName %> { + awesomeThings:any[] = [ + 'HTML5 Boilerplate', + 'AngularJS', + 'Karma' + ]; + } +} + angular.module('<%= scriptAppName %>') - .service('<%= classedName %>', function <%= classedName %>() { - // AngularJS will instantiate a singleton by calling "new" on this function - }); + .service('<%= cameledName %>', <%= scriptAppName %>.<%= classedName %>);