Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScriptで書くAngularJS @ GDG神戸2014.8.23

TypeScriptで書くAngularJS @ GDG神戸2014.8.23

2014年8月23日発表分の資料をSlideShareより再掲。
http://www.slideshare.net/armorik83/gdgkobe-angularjs-2

OKUNOKENTARO

August 23, 2014
Tweet

More Decks by OKUNOKENTARO

Other Decks in Programming

Transcript

  1. class Greeter { private greeting: string; constructor(message: string) { this.greeting

    = message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); TS
  2. var Greeter = (function () { function Greeter(message) { this.greeting

    = message; } Greeter.prototype.greet = function () { return 'Hello, ' + this.greeting; }; return Greeter; })(); ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = function () { alert(greeter.greet()); }; document.body.appendChild(button); //# sourceMappingURL=greeter.js.map JS
  3. class Greeter { private greeting: string; constructor(message: string) { this.greeting

    = message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); TS
  4. class Greeter { private greeting: string; constructor(message: string) { this.greeting

    = message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); ̒䒷侧ך㘗 ،ؙإأ⥜귅㶨̓ OFX דㄎל׸׷̓ ֿך鴟כ֮ת׶㢌׻׵זְ ̓،ٗ٦ꟼ侧䒭&4⯓《׶ TS
  5. ׉ך➭ך暴䗙 ˖ 㘗䱿锷֮׶תׅ ˖ var foo = 'abc';
 var foo:

    string = 'abc';ה剅ַזֻג׮ְְ ˖ JOUFSGBDFד㹋鄲ךזְ㘗ך׫ך㹀纏 ˖ NPEVMFדせ⵸瑞꟦涸ז⢪ְ倯
 ֿֿדְֲNPEVMFכ$PNNPO+4הכ殯ז׷ ˖ آؑطؙٔأծ竰䪫ծזוזו
  6. <body ng-app> <div ng-controller="SampleCtrl"> <strong>First name:</strong> {{firstName}}<br> </div> </body> HTML

    JS function SampleCtrl ($scope) { $scope.firstName = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; };
  7. <body ng-app> <div ng-controller="sampleCtrl"> <strong>First name:</strong> {{firstName}}<br> </div> </body> HTML

    JS ! 5ZQF4DSJQUכ♶銲הְֲ倯׮ ֿך剅ֹ倯כװ׭ת׃׳ֲ ֮ה֮הخְٓדׅ ̕ function SampleCtrl ($scope) { $scope.firstName = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; };
  8. angular.module('MyAngularJs', [/*...*/]); // Setter ! function SampleCtrl ($scope) { $scope.firstName

    = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; ! angular.module('MyAngularJs') // Getter .controller('SampleCtrl', [ '$scope', // 使用するServiceは全て文字列で表記 SampleCtrl ]); JS BOHVMBSNPEVMF ׾⢪ְת׃׳ֲ
  9. angular.module('MyAngularJs', [/*...*/]); // Setter ! function SampleCtrl ($scope) { $scope.firstName

    = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; ! angular.module('MyAngularJs') // Getter .controller('SampleCtrl', [ '$scope', // 使用するServiceは全て文字列で表記 SampleCtrl ]); JS ֿֿ̓׾5ZQF4DSJQUח׃תׅ
  10. TS // ... class SampleCtrl { constructor ( public $scope

    ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]);
  11. // ... class SampleCtrl { constructor ( public $scope )

    { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); TS ̒׍׳׏ה孡䭯׍䝤ְַ׮׃׸זְ խֽוծ"OHVMBS+4ך䒷侧כ㟓ִָ׍
 խזךד何遤׃גֶֻ ̓،ٗ٦ꟼ侧זךדUIJTָ䭷ׅⰻ㺁כ
 խ㢌׻׵זְ@UIJTהַ׃זֻגְְ ̕DMBTT׾㹑鎉׃תׅ DMBTTך؝ٝأزؙٓةָ床׷̓
  12. class SampleCtrl { constructor ( public $scope ) { //

    ... } } ! class SampleCtrl { public $scope; constructor ($scope) { this.$scope = $scope; // ... } } TS ずֿׄה׾ ׃גתׅ ♳ךקֲָءٝفٕ
  13. TS /// <reference path="angularjs/angular.d.ts" /> ! class SampleCtrl { constructor

    ( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); ̓⿫撑׾㹑鎉ׅ׷ה ̕؝ٝػ؎ָٓ㢌侧BOHVMBS׾钠陎㘗➰ֹד
  14. TS declare var angular: any; ! class SampleCtrl { constructor

    ( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); ̓EFDMBSFהְֲչ،ٝؽؒٝز㹑鎉պ׾欽ְ׷ה խ㘗㹀纏ؿ؋؎ָٕ搀ֻג׮؝ٝػ؎ٓח侄ִ׵׸׷ խ؝ٝػ؎ٕ穠卓חכ䕦갟׃זְ
  15. TS declare var angular: any; ! class SampleCtrl { constructor

    ( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]);
  16. BOZ㘗 ˖ 5ZQF4DSJQUחכOVNCFS TUSJOH CPPMFBOה ְֲفٔىذ؍ـ㘗ָ欽䠐ׁ׸גְ׷ ˖ ׉ך➭ծWPJE OVMM VOEFOFE㘗׮欽䠐ׁ׸גְ׷

    ֿ׸׵ך㘗דך㢌侧㹑鎉כדֹזְ ˖ BOZ㘗כ䖞勻ך+BWB4DSJQUך㘗 ˖ ؝ٝػ؎ٕؒٓ٦׾װ׬׾䖤׆럀׵ׇ׷㜥さװ
 㹀纏ؿ؋؎ٕ׾锃麦דֹזְ㜥さזוח
  17. TS // ... ! class SampleCtrl { constructor ( public

    $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! // ...
  18. TS // ... ! class SampleCtrl { constructor ( public

    $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! // ... ! ͕͜͜૿͑ͦ͏ͳͷ͸ ໨ʹݟ͍͑ͯΔʜ
  19. TS constructor ( public $scope ) { this.init(); ! this.$scope.getFullName

    = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } ! private init() { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; }
  20. TS constructor ( public $scope ) { this.init(); } !

    private init() { // ... } ! public getFullName(): string { return this.$scope.firstName + ' ' + this.$scope.lastName; } ̕䨱׶⦼ך㘗
  21. ?!

  22. TS constructor ( public $scope ) { this.init(); this.$scope.getFullName =

    this.getFullName.bind(this); } ! private init() { // ... } ! public getFullName(): string { return this.$scope.firstName + ' ' + this.$scope.lastName; } 姻׃ֻכCJOE ׾⢪ְתׅ
  23. <body ng-app> <div ng-controller="SampleCtrl as c"> <strong>First name:</strong> {{c.firstName}}<br> <strong>Last

    name:</strong> {{c.lastName}}<br> <strong>Full name:</strong> {{c.getFullName()}}<br> </div> </body> HTML
  24. <body ng-app> <div ng-controller="SampleCtrl as c"> <strong>First name:</strong> {{c.firstName}}<br> <strong>Last

    name:</strong> {{c.lastName}}<br> <strong>Full name:</strong> {{c.getFullName()}}<br> </div> </body> HTML ̕呓秛ׅ׷㢌侧せ ̓㢌侧せفٗػذ؍ խ➰ֽזְהSPPU4DPQF䪔ְ
  25. TS class SampleCtrl { public firstName: string; public lastName: string;

    ! constructor () { this.init(); } ! private init() { this.firstName = 'John'; this.lastName = 'Doe'; } ! public getFullName(): string { return this.firstName + ' ' + this.lastName; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', [SampleCtrl]);
  26. TS class SampleCtrl { public firstName: string; public lastName: string;

    ! constructor () { this.init(); } ! private init() { this.firstName = 'John'; this.lastName = 'Doe'; } ! public getFullName(): string { return this.firstName + ' ' + this.lastName; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', [SampleCtrl]); ̕TDPQF♶銲 ̒TDPQF♶銲 ̒UIJTך湫䱸ךفٗػذ؍ח խז׷ךד㘗׾㹀纏
  27. 'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);

    }]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS
  28. 'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);

    }]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS ̓؎ٝأةٝأ׾欰䧭׃ג鵤ׅ ؝ٝأزؙٓة׾♷ִג"OHVMBS+4ָ欰䧭ׅ׷̓ 4JOHMFUPOדׅխ  խ
  29. 'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);

    }]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS 5ZQF4DSJQUד 䪔ְװְׅךכ4FSWJDF
  30. TS class MyResource { constructor( private $q: ng.IQService, private $resource:

    ng.resource.IResourceService ) { // ... } ! private resource(): IResourceClass { var baseApi = '/api/entities'; var params: any = null; var actions = { getWithEntityId: { method: 'GET', url: baseApi+'/:id', isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public fetchEntity(id: my.obj.EntityId): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( query, this.success(), this.failure() ).$promise.then(this.fetchThen(dfd)); ! return dfd.promise; } private fetchThen(dfd: IDeferredAcceptEntity): (res: my.obj.IEntities) => void { return (res) => { var e: my.obj.IEntity = res[0]; dfd.resolve(e); }; } } ! angular.module('MyAngularJs') .service('MyResource', ['$q', '$resource', MyResource]);
  31. class MyResource { constructor( private $q: ng.IQService, private $resource: ng.resource.IResourceService

    ) { // ... } ! private resource(): IResourceClass { var baseApi = '/api/entities'; var params: any = null; var actions = { getWithEntityId: { method: 'GET', url: baseApi+'/:id', isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public fetchEntity(id: my.obj.EntityId): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( TS ̕BOHVMBSEUTך㘗㹀纏ؿ؋؎ٕ׾崞欽 ٍ̓ؗأز׃זְה鋵ך㘗ה׃גؒٓ٦׾⳿ׅ ̕ⴽךؿ؋؎ٕד杝荈ח*3FTPVSDF$MBTT׾㹀纏 խBOHVMBSEUT׾FYUFOET竰䪫ׇׁגְ׷
  32. isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public

    fetchEntity(id: number): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( query, this.success(), this.failure() ).$promise.then(this.fetchThen(dfd)); ! return dfd.promise; } private fetchThen(dfd: IDeferredAcceptEntity): (res: my.obj.IEntities) => void { return (res) => { var e: my.obj.IEntity = res[0]; dfd.resolve(e); }; } } ! angular.module('MyAngularJs') .service('MyResource', ['$q', '$resource', MyResource]); TS ֿֿתדָⰋג䨱׶⦼ך㘗 ̕杝荈ך㘗 ̓杝荈ך㘗 ̕%*ׅ׷1SPWJEFSה荈魦ך؝ٝأزؙٓة խ䧭⸆儗ה㣟侁儗חㄎל׸׷ꟼ侧 ̒➙㔐כꟼ⤘זְ
  33. BOHVMBSEUT ng.resource.IResourceClass<T>型のインスタンス .query(); ↓ ng.resource.IResourceArray<T>型のインスタンス .$promise ↓ ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス .then<TResult>( successCallback:

    (promiseValue: T) => TResult, errorCallback?: (reason: any) => TResult, notifyCallback?: (state: any) => any ): IPromise<TResult> 殅䠐ׅץֹזךכծֿך鴟כ Ⰻ鿇דչמהאך㘗せպד֮׷הְֲֿה չ5׾䒷侧חה׶53FTVMU׾鵤ׅꟼ侧պ׾䒷侧הׅ׷