How to create components from a JSON array in cycle js

拟墨画扇 提交于 2019-12-06 11:33:29

Starting from the Cycle.js example, and focusing in on the main function, here's a reworking to accommodate the slider controls and values originating in an array:

function main(sources) {

  let labeledSliderArray = [
    {label: 'Weight', unit: 'kg',    min: 40,  max: 150, init: 70},
    {label: 'Height', unit: 'cm',    min: 140, max: 220, init: 170},
    {label: 'Age',    unit: 'years', min: 10,  max: 80,  init: 20}
  ];

  vtrees = labeledSliderArray.map(function (item) {
    const itemProps$ = Rx.Observable.of(item);  
    const itemSinks = IsolatedLabeledSlider({
      DOM: sources.DOM, props: itemProps$
    });
    return itemSinks.DOM;
  });

  const vtree$ = Rx.Observable.combineLatest(
    vtrees, (...vtreeargs) =>
      div(vtreeargs)
  );

  return {
    DOM: vtree$
  };
}

The labeledSliderArray is mapped over and in that loop the *Props$, *Sinks, and *VTree$ are generalized, then the resulting array is provided to combineLatest. Next, the es6 "rest parameters" operator is used to provide the list of resulting vtrees to the hyperscript div function making up the final vtree$ reactive stream.

JSBin Demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!