Mainly Devel Notes

Twitter, GitHub, StackOverflow: @ovrmrw (short hand of "overmorrow" that means the day after tomorrow)

ko.computed(Knockout) equivalent in Angular + TypeScript

sometimes it is calculated by Angular, sometimes you fill it by hand.

(English)
My most familiar JavaScript library is KnockoutJS.
Especially I like ko.computed that is a mighty feature of Knockout. When observed variables are changed, Knockout triggers Dependency Tracking chain. Then all variables needed to update will be updated automatically.
This sample is for a typical case of business apps with ko.computed equivalent in Angular. (+ TypeScript)

(Japanese)
Angularでko.computed(KnockoutJS)のようなことをやってみました。
$scope.$watchを使ってとりあえず目的は果たしましたが、rateLimit相当のやり方はまだわかっていません。今後の課題です。
今回のサンプルは業務システムでよくある数量、単価、金額の扱いに関するものです。通常は数量と単価を掛けて金額としますが、実運用上では単価無しで数量と金額だけ入力するというケースもあります。
また、TypeScriptを使うことでJavaScriptにありがちな追跡困難なエラーとの不本意な長期戦を避けられます。Angular 2での採用も決まっていますのでまだの人は是非。

(jsFiddle)

(index.html and myScript.ts on Gist)

Angular Sample - Calculating Amount with Quantity and Price