From 8ddbc39f4af446db65aa587bb3137577c94100d9 Mon Sep 17 00:00:00 2001 From: Karol Sluszniak Date: Tue, 31 Mar 2015 10:45:20 +0200 Subject: [PATCH] Refine README some more --- README.md | 18 ++++++++++++++---- angular-computed.js | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8d69778..ca44d9d 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ Computed property concept allows to produce more efficient and elegant scope properties. With **$computed** you get an easy way to define them using pattern similar to Dependency Injection, well known to all Angular developers. Here are some advantages: -- views are simplified as you can use properties instead of function calls -- efficiency is gained as computation function is invoked only once after variable change +- property computation is executed just once after input change +- property computation is executed just once even if property is used in multipe places +- views are simplified with `{{ var }}` instead of `{{ computeVar() }}` - computed properties are visually separated in controller code - definition syntax is consistent with core Angular concepts @@ -11,7 +12,7 @@ Internally, **$computed** is very simple and mostly based on `$watch` ability of ## Usage -First off, add `angular-computed.js` file to your project. You can download it from [here](https://raw.githubusercontent.com/karolsluszniak/angular-computed/master/angular-computed.js) or require bower `angular-computed` package. +First off, add **angular-computed.js** file to your project. You can download it from [here](https://raw.githubusercontent.com/karolsluszniak/angular-computed/master/angular-computed.js) or require bower `angular-computed` package. Then, add `ngComputed` as your app's dependency: @@ -105,7 +106,7 @@ Guessing variable names from function signature, like Angular does in its DI, is ### Dependency chain -You can add computed properties that are dependent on other computed properties. In fact, this is where **$computed** really starts to shine with its performance benefits. +You can add computed properties that are dependent on other computed properties. In fact, this is where the pattern implemented by **$computed** really starts to shine with its performance benefits. ```js app.controller('AppCtrl', ['$computed', function($computed) { @@ -125,6 +126,15 @@ app.controller('AppCtrl', ['$computed', function($computed) { }]); ``` +Imagine something like this happens later in controller lifecycle: + +```js +this.a = this.a + 1; +this.b = this.b - 1; +``` + +The `sum` property will be recomputed due to input change but its result will not change and thus the `sumPow` property will not have to be recomputed. + ## Contributing 1. Fork it (https://github.com/karolsluszniak/angular-computed/fork) diff --git a/angular-computed.js b/angular-computed.js index 750d091..35d1d9e 100644 --- a/angular-computed.js +++ b/angular-computed.js @@ -27,7 +27,7 @@ return angular.bind(context, item); } }), function() { - context[name] = valueFunc.apply(context, arguments[0], arguments[1]); + context[name] = valueFunc.apply(context, arguments[0]); }); }; }]).