Skip to content

Commit

Permalink
fix(toggle): do not fire change when initializing
Browse files Browse the repository at this point in the history
Closes #6144
  • Loading branch information
adamdbradley committed May 19, 2016
1 parent 643d426 commit cd2afb3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
12 changes: 11 additions & 1 deletion ionic/components/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const CHECKBOX_VALUE_ACCESSOR = new Provider(
})
export class Checkbox {
private _checked: boolean = false;
private _init: boolean;
private _disabled: boolean = false;
private _labelId: string;
private _fn: Function;
Expand Down Expand Up @@ -127,7 +128,9 @@ export class Checkbox {
private _setChecked(isChecked: boolean) {
if (isChecked !== this._checked) {
this._checked = isChecked;
this.change.emit(this);
if (this._init) {
this.change.emit(this);
}
this._item && this._item.setCssClass('item-checkbox-checked', isChecked);
}
}
Expand Down Expand Up @@ -185,6 +188,13 @@ export class Checkbox {
*/
onTouched() {}

/**
* @private
*/
ngAfterContentInit() {
this._init = true;
}

/**
* @private
*/
Expand Down
8 changes: 8 additions & 0 deletions ionic/components/toggle/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class E2EApp {
this.grapeDisabled = !this.grapeDisabled;
}

appleChange(ev) {
console.log('appleChange', ev);
}

bananaChange(ev) {
console.log('bananaChange', ev);
}

kiwiChange(ev) {
console.log('kiwiChange', ev);
this.kiwiValue = ev.checked;
Expand Down
4 changes: 2 additions & 2 deletions ionic/components/toggle/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

<ion-item>
<ion-label>Apple, ngControl</ion-label>
<ion-toggle ngControl="appleCtrl"></ion-toggle>
<ion-toggle ngControl="appleCtrl" (change)="appleChange($event)"></ion-toggle>
</ion-item>

<ion-item>
<ion-label>Banana, ngControl</ion-label>
<ion-toggle ngControl="bananaCtrl"></ion-toggle>
<ion-toggle ngControl="bananaCtrl" (change)="bananaChange($event)"></ion-toggle>
</ion-item>

<ion-item>
Expand Down
13 changes: 12 additions & 1 deletion ionic/components/toggle/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const TOGGLE_VALUE_ACCESSOR = new Provider(
})
export class Toggle implements ControlValueAccessor {
private _checked: boolean = false;
private _init: boolean;
private _disabled: boolean = false;
private _labelId: string;
private _activated: boolean = false;
Expand Down Expand Up @@ -191,9 +192,12 @@ export class Toggle implements ControlValueAccessor {
* @private
*/
private _setChecked(isChecked: boolean) {
console.debug('_setChecked')
if (isChecked !== this._checked) {
this._checked = isChecked;
this.change.emit(this);
if (this._init) {
this.change.emit(this);
}
this._item && this._item.setCssClass('item-toggle-checked', isChecked);
}
}
Expand Down Expand Up @@ -248,6 +252,13 @@ export class Toggle implements ControlValueAccessor {
*/
onTouched() {}

/**
* @private
*/
ngAfterContentInit() {
this._init = true;
}

/**
* @private
*/
Expand Down

0 comments on commit cd2afb3

Please sign in to comment.