-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
99 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ unnecessarily. | |
- [EnumKeyFormatPipe](#enumkeyformatpipe) | ||
- [Helper Classes](#helper-classes) | ||
- [RxDestroy](#rxdestroy) | ||
- [OnChange](#onchange) | ||
|
||
## Installation | ||
|
||
|
@@ -288,6 +289,11 @@ interval(1000) | |
.subscribe(_ => {}); | ||
``` | ||
|
||
### OnChange | ||
|
||
A decorator for change detection on properties | ||
https://blog.angularindepth.com/creatively-decouple-ngonchanges-fab95395cc6e | ||
|
||
## License | ||
|
||
MIT © [Jaspero Ltd](mailto:[email protected]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Courtesy of | ||
* https://blog.angularindepth.com/creatively-decouple-ngonchanges-fab95395cc6e | ||
*/ | ||
|
||
export interface SimpleChange<T> { | ||
firstChange: boolean; | ||
previousValue: T; | ||
currentValue: T; | ||
isFirstChange: () => boolean; | ||
} | ||
|
||
export function OnChange<T = any>( | ||
callback: (value: T, simpleChange?: SimpleChange<T>) => void | ||
) { | ||
let _cachedValue: T; | ||
let _isFirstChange = true; | ||
|
||
return (target: any, key: PropertyKey) => { | ||
Object.defineProperty(target, key, { | ||
set: function(value) { | ||
// No operation if new value is same as old value | ||
if (!_isFirstChange && _cachedValue === value) { | ||
return; | ||
} | ||
const oldValue = _cachedValue; | ||
_cachedValue = value; | ||
const simpleChange: SimpleChange<T> = { | ||
firstChange: _isFirstChange, | ||
previousValue: oldValue, | ||
currentValue: _cachedValue, | ||
isFirstChange: () => _isFirstChange | ||
}; | ||
_isFirstChange = false; | ||
callback.call(this, _cachedValue, simpleChange); | ||
}, | ||
get: function() { | ||
return _cachedValue; | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
<div *ngFor="let i of test; jpTrackByField:'pero'">{{i.id}}</div> | ||
|
||
<jp-on-changes [value]="onChangeValue"></jp-on-changes> | ||
|
||
<button (click)="changeOnChangeValue()"> | ||
Change value | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { Component } from '@angular/core'; | ||
import {Component} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
selector: 'jp-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.scss'] | ||
}) | ||
export class AppComponent { | ||
test = Array.from(Array(20).keys()).map(i => ({id: i, label: 'bla' + i})); | ||
|
||
onChangeValue = Math.random(); | ||
|
||
changeOnChangeValue() { | ||
this.onChangeValue = Math.random(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<span>{{value}}</span> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {Component, Input} from '@angular/core'; | ||
import {OnChange} from '@jaspero/ng-helpers'; | ||
|
||
@Component({ | ||
selector: 'jp-on-changes', | ||
templateUrl: './on-changes.component.html', | ||
styleUrls: ['./on-changes.component.scss'] | ||
}) | ||
export class OnChangesComponent { | ||
@OnChange<number>(function(value, simpleChange) { | ||
console.log(value, simpleChange); | ||
}) | ||
@Input() | ||
value: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
{ | ||
"extends": "../tslint.json", | ||
"rules": { | ||
"directive-selector": [ | ||
true, | ||
"attribute", | ||
"app", | ||
"camelCase" | ||
], | ||
"component-selector": [ | ||
true, | ||
"element", | ||
"app", | ||
"kebab-case" | ||
] | ||
} | ||
"extends": "../tslint.json", | ||
"rules": { | ||
"directive-selector": [true, "attribute", "jp", "camelCase"], | ||
"component-selector": [true, "element", "jp", "kebab-case"] | ||
} | ||
} |