Skip to content

Commit

Permalink
Merge c0810a1 into 244824a
Browse files Browse the repository at this point in the history
  • Loading branch information
damyanpetev committed Jan 24, 2018
2 parents 244824a + c0810a1 commit 0d54adc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/igcombo/igcombo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@ export class IgComboComponent extends IgControlBase<IgCombo> implements ControlV
ngOnInit() {
let that = this;
super.ngOnInit();
jQuery(this._el).on(this._widgetName.toLowerCase() + "selectionchanged", function (evt, ui) {
var items = ui.items;

if (items.length <= 0 || !that._model) {
that._model.viewToModelUpdate(ui.owner.value());
return;
}

if (ui.owner.options.multiSelection.enabled) {
that._model.viewToModelUpdate(items.map(function(item) {
return item.data[that._config.valueKey];
}));
} else {
that._model.viewToModelUpdate(items[0].data[that._config.valueKey]);
}
});
this._dataSource = jQuery.extend(true, [], this._config.dataSource);
//manually call writeValue, because the LifeCycle has been changed and writeValue is executed before ngOnInit

if (this._model) {
// D.P. #244 only attach selectionchanged handler if there's a model to update
jQuery(this._el).on(this._widgetName.toLowerCase() + "selectionchanged", function (evt, ui) {
var items = ui.items;

if (items.length <= 0) {
that._model.viewToModelUpdate(ui.owner.value());
return;
}

if (ui.owner.options.multiSelection.enabled) {
that._model.viewToModelUpdate(items.map(function(item) {
return item.data[that._config.valueKey];
}));
} else {
that._model.viewToModelUpdate(items[0].data[that._config.valueKey]);
}
});
//manually call writeValue, because the LifeCycle has been changed and writeValue is executed before ngOnInit
this.writeValue(this._model.value);
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/igcombo/combo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ export function main() {
});
});

it('should select correctly without ngModel', (done) => {
const template = '<div><ig-combo [(widgetId)]="comboID" [(options)]="options"></ig-combo></div>';
TestBed.overrideComponent(TestComponent, {
set: {
template: template
}
});
TestBed.compileComponents().then(() => {
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
const elem = $("#combo1").igCombo("itemsFromIndex", 0)["element"];
const select = () => {
$("#combo1").igCombo("select", elem, {}, true);
}

// #244 fails with 'Cannot read property 'viewToModelUpdate' of undefined'
expect(select).not.toThrow();
expect(fixture.componentInstance.viewChild.value()).toEqual(1);
done();
});
});


it('should be updated correctly if the ngModel value is updated', (done) => {
var template = '<div><ig-combo [(widgetId)]="comboID" [(options)]="options" [changeDetectionInterval]="cdi" [(ngModel)]="combo.value1"></ig-combo></div>';
TestBed.overrideComponent(TestComponent, {
Expand Down

0 comments on commit 0d54adc

Please sign in to comment.