给定来自 Cycle js 网站的标记滑块组件示例。我将如何获取滑块信息对象的 JSON 数组并为数组中的每个对象创建一个带标签的滑块组件。例如,如果我有一个这样的数组
let labelSliderArray =
[
{
label: 'Height',
unit: 'in',
min: 40,
max: 84,
init: 50
},
{
label: 'Weight',
unit: 'ibs',
min: 40,
max: 84,
init: 50
},
{
label: 'Age',
unit: 'years',
min: 10,
max: 65,
init: 20
}
]
我如何将每个标签对象映射到标签组件。我试图将每个标签对象映射到 IsolatedLabeledSlider,就像这样
const {div, input, label, h2, makeDOMDriver} = CycleDOM;
const isolate = CycleIsolate;
function intent(DOM){
return DOM.select('.slider').events('input')
.map(ev => ev.target.value);
};
function model(action$, props$){
const intialValue$ = props$.map(props => props.init).first();
const value$ = intialValue$.concat(action$)
const state$ = Rx.Observable.combineLatest(value$, props$,
(value,props) => {
return {
label: props.label,
unit: props.unit,
min: props.min,
max: props.max,
value: value
}
}
);
return state$
};
function view(state$){
return state$.map( state =>
div('.labled-slider',
[
label('.label', `${state.label}: ${state.value} ${state.unit}`),
input('.slider', {type: 'range', min: state.min, max: state.max, valeu : state.value})
]
)
)
};
function LabeledSlider(sources) {
const change$ = intent(sources.DOM);
const state$ = model(change$, sources.props)
const vtree$ = view(state$)
return {
DOM: vtree$
};
};
const IsolatedLabelSlider = function (sources){
return isolate(LabeledSlider)(sources)
}
function main(sources){
let labelSliderArray = Rx.Observable.from(
[
{
label: 'Height',
unit: 'in',
min: 40,
max: 84,
init: 50
},
{
label: 'Weight',
unit: 'ibs',
min: 40,
max: 84,
init: 50
},
{
label: 'Age',
unit: 'years',
min: 10,
max: 65,
init: 20
}
]);
let labelSinks = labelSliderArray.map(sProps => IsolatedLabelSlider({DOM: sources.DOM, props: Rx.Observable.of(sProps)}) )
let labelVtree$ = labelSinks.map(l => l.DOM)
return {
DOM: labelVtree$
};
}
const drivers = {
DOM: makeDOMDriver('#app')
}
Cycle.run(main, drivers);
但这失败了,它只渲染了数组中的最后一个对象,任何帮助都会很棒
这是来自 Cycle JS 网站的示例