Skip to content

Commit

Permalink
Refine README some more
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsluszniak committed Mar 31, 2015
1 parent 9750c9b commit 8ddbc39
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

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

Internally, **$computed** is very simple and mostly based on `$watch` ability of Angular Scope. It's just a tiny, clean pattern implementation.

## 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:

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion angular-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
};
}]).
Expand Down

0 comments on commit 8ddbc39

Please sign in to comment.