Ng2-select: Change the behavior on set items instead of on initialization

Created on 19 Jan 2016  ·  3Comments  ·  Source: valor-software/ng2-select

The behavior is set on initialization, but sometimes the items are not available on initialization and they are only received a bit afterwords causing errors since the behavior is governed by the first item.
instead you can move the logic to the items setter

  @Input() set items(value:Array<any>) {
    this._items = value;
    this.itemObjects = this._items.map((item:any) => new SelectItem(item));
    this.behavior = this.itemObjects.length > 0 && this.itemObjects[0].hasChildren() ?
      new ChildrenBehavior(this) : new GenericBehavior(this);
  }

(maybe you could somehow check if the previous behavior is like the new one for not creating a new behavior all the time, or just create both lazily and store them, then switch between the two if the items change)

Most helpful comment

Running into this issue as well, trying to use with async loaded data and is erroring out :/

All 3 comments

I was thinking to rewrite all this to observables
so Items will be checked only after typing
this will increase overall load spead

Running into this issue as well, trying to use with async loaded data and is erroring out :/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hasnain-Bukhari picture Hasnain-Bukhari  ·  3Comments

Garybhardwaj picture Garybhardwaj  ·  3Comments

carstenschaefer picture carstenschaefer  ·  5Comments

bissolli picture bissolli  ·  6Comments

cherrydev picture cherrydev  ·  5Comments