refactor(toggle): simplified onChange method

This commit is contained in:
Manu Mtz.-Almeida
2016-10-01 01:43:23 +02:00
parent d02e14c50a
commit e86cb1467a

View File

@ -77,7 +77,7 @@ export class Toggle extends Ion implements AfterContentInit, ControlValueAccesso
/** @private */ /** @private */
_checked: boolean = false; _checked: boolean = false;
/** @private */ /** @private */
_init: boolean; _init: boolean = false;
/** @private */ /** @private */
_disabled: boolean = false; _disabled: boolean = false;
/** @private */ /** @private */
@ -89,7 +89,7 @@ export class Toggle extends Ion implements AfterContentInit, ControlValueAccesso
/** @private */ /** @private */
_msPrv: number = 0; _msPrv: number = 0;
/** @private */ /** @private */
_fn: Function; _fn: Function = null;
/** @private */ /** @private */
_events: UIEventManager = new UIEventManager(); _events: UIEventManager = new UIEventManager();
@ -229,18 +229,14 @@ export class Toggle extends Ion implements AfterContentInit, ControlValueAccesso
*/ */
registerOnChange(fn: Function): void { registerOnChange(fn: Function): void {
this._fn = fn; this._fn = fn;
this.onChange = (isChecked: boolean) => {
console.debug('toggle, onChange', isChecked);
fn(isChecked);
this._setChecked(isChecked);
this.onTouched();
};
} }
/** /**
* @private * @private
*/ */
registerOnTouched(fn: any) { this.onTouched = fn; } registerOnTouched(fn: any) {
this.onTouched = fn;
}
/** /**
* @input {boolean} whether the toggle is disabled or not * @input {boolean} whether the toggle is disabled or not
@ -260,7 +256,8 @@ export class Toggle extends Ion implements AfterContentInit, ControlValueAccesso
*/ */
onChange(isChecked: boolean) { onChange(isChecked: boolean) {
// used when this input does not have an ngModel or formControlName // used when this input does not have an ngModel or formControlName
console.debug('toggle, onChange (no ngModel)', isChecked); console.debug('toggle, onChange', isChecked);
this._fn && this._fn(isChecked);
this._setChecked(isChecked); this._setChecked(isChecked);
this.onTouched(); this.onTouched();
} }
@ -289,6 +286,7 @@ export class Toggle extends Ion implements AfterContentInit, ControlValueAccesso
ngOnDestroy() { ngOnDestroy() {
this._form.deregister(this); this._form.deregister(this);
this._events.unlistenAll(); this._events.unlistenAll();
this._fn = null;
} }
} }