Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Constructor args #13

Merged
merged 14 commits into from
Apr 11, 2018
Merged

Constructor args #13

merged 14 commits into from
Apr 11, 2018

Conversation

valdrinkoshi
Copy link
Collaborator

@valdrinkoshi valdrinkoshi commented Apr 10, 2018

Addresses #8 by allowing to set one-time properties like container, layout, newChildFn, etc only through the constructor.
Addresses #12 by removing Fn suffix from newChild/updateChild/recycleChild

Initializing VirtualRepeater requires passing at minimum {container, newChild} in the config

const repeater = new VirtualRepeater({
  container: document.body,
  newChild: (item, idx) => {
    const child = document.createElement('div');
    child.textContent = idx;
    return child;
  }
});
repeater.items = new Array(100).fill('item');
repeater.first = 0;
repeater.num = 5;

Initializing VirtualList requires passing at minimum {container, layout, newChild}

const list = new VirtualList({
  layout: new Layout(),
  container: document.body,
  newChild: (item, idx) => {
    const child = document.createElement('div');
    child.textContent = idx;
    return child;
  }
});
list.items = new Array(100).fill('item');

It is still possible to extend both and override any of these configs by overriding either the container, layout setters or the child creation methods, e.g.

class MyList extends VirtualList {
  get _container() { return document.body; }
  set _container(_) {}

  get _layout() { return this.__layout; }
  set _layout(_) { this.__layout = this.__layout || new Layout(); }

  _newChild() { return document.createElement('input') }
}

Custom elements examples have been updated to keep an internal repeater/list instance instead of using the mixins. They forward the setting of public properties to the internal repeater/list e.g. num, first, items.

<virtual-list id="listEl" direction="horizontal"></virtual-list>
<script>
  listEl.items = new Array(10).fill('http://lorempixel.com/200/200/');
  listEl.template = {
    newChild: (item) => {
      const img = document.createElement('img');
      img.width = img.height = 200;
      img.src = item;
      return img;
    }
  };
</script>

@valdrinkoshi valdrinkoshi changed the base branch from design to master April 10, 2018 18:55
@valdrinkoshi valdrinkoshi merged commit 6bf9594 into master Apr 11, 2018
@valdrinkoshi valdrinkoshi deleted the constructor-args branch April 12, 2018 19:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant