Skip to content

Commit

Permalink
Merge branch 'stateful-filters'
Browse files Browse the repository at this point in the history
  • Loading branch information
PillowPillow committed Aug 20, 2015
2 parents 6e5216e + e0cf61d commit e4dd213
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
42 changes: 41 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
## 0.1.12 (2015-08-20)

Update:
* @filter

> the $stateful property is now deprecated
> use the stateful parameter instead
*before*

````javascript
import {filter, inject} from 'node_modules/ng-annotations';

@filter('statefulFilter')
@inject('someDependency')
class StatefulFilter {
$stateful = true;
$filter(input) {
return input; //do something with the dependency
}
}

````

*after*

````javascript
import {filter, inject} from 'node_modules/ng-annotations';

@filter({name: 'statefulFilter', stateful:true})
@inject('someDependency')
class StatefulFilter {
$filter(input) {
return input; //do something with the dependency
}
}

````


## 0.1.11 (2015-08-20)

Bugfix:
Feature:
* @filter

> now supports the stateful filters
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-annotations",
"version": "0.1.11",
"version": "0.1.12",
"description": "angular wrapper based on annotations",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,9 @@ export default class SomeRun {
#### type: *function*
#### Params:
- **name**: *(Optional)* String. angular filter name, by default the decorator will take the class name.
- **properties**: *(Optional)* Object|String. angular filter properties. contains the name and the stateful attribute
- name: String. angular filter name, by default the decorator will take the class name.
- stateful: Boolean. default false

#### Usage:
> The decorated filter is slightly different than the original.
Expand All @@ -571,17 +573,16 @@ export default class Capitalize {
````

#### Note:
> If you need to write a stateful filter, you have to mark the filter as `$stateful` by defining a $stateful property to true
> If you need to write a stateful filter, you must give a literal objet as parameter to the filter decorator
````javascript
//inspired by https://docs.angularjs.org/guide/filter
import {filter, inject, attach} from 'node_modules/ng-annotations';

@filter('decorate')
@filter({name:'decorate', stateful:true})
@inject('decoration')
export default Decorate {

$stateful=true;
@attach('decoration', 'symbol')
decorator;

Expand Down
23 changes: 19 additions & 4 deletions src/decorators/components/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import inject from 'src/decorators/utils/inject';
*
* declares a new angular filter
*
* @param name (optional) replaces the class name
* @param filterProps (optional) filter properties containing name and the stateful attribute
*
* @returns {Function}
*/
export default function NgFilter(name = '') {
export default function NgFilter(filterProps = {name:'',stateful:false}) {

return (target) => {
name = name || target.name;

let name = '', stateful = false;
if(filterProps instanceof Object){
name = filterProps.name || target.name;
stateful = !!filterProps.stateful;
}
else
name = filterProps || target.name;

var component = function(...injections) {
let instance = new target(...injections);
Expand All @@ -22,8 +30,15 @@ export default function NgFilter(name = '') {
throw Error('an annotated "filter" must implement the "$filter" method');
utils.applyTransformations(target, instance, injections);

if(instance.$stateful === true)
//@todo remove it in the next version
if(instance.$stateful === true) {
console.warn('the $stateful property is deprecated and will be removed in the next versions, use the @filter parameter instead');
console.warn('https://github.com/PillowPillow/ng-annotations#d_filter');
filter.$stateful = true;
}

if(stateful)
filter.$stateful = stateful;

return filter;
function filter(...parameters) {
Expand Down

0 comments on commit e4dd213

Please sign in to comment.