我在做什么我认为在你的问题中更多的是 1. 和 2. 的组合。所有标记都在 HTML 文件中,但不是静态的。这是snabbdom-template模块用来动态填充自定义数据然后返回给 CycleJS 以呈现到 DOM 的模型。
main.js
const xs = require('xstream').default
const run = require('@cycle/run').run
const makeDOMDriver = require('@cycle/dom').makeDOMDriver
const st = require('snabbdom-template')
const fs = require('fs')
const template = fs.readFileSync('template.html', 'utf-8')
function main(sources) {
return {
DOM: xs.of(['one','two','three','four']) // mock data
.map(list =>
st(template, { // call snabbdom-template
'div#message': {class: 'myclass', '_html': 'Ready.'}, // link selectors and data
'#mapme': {_map: {'li': list}}
})
)
}
}
run(main, {
DOM: makeDOMDriver('#main-container')
})
给定index.html
<html>
<body>
<div id="main-container"></div>
<script src="bundle.js"></script>
</body>
</html>
...和template.html
<div id="message">message goes here</div>
<ul id="mapme">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
...产生类似的东西
<html>
<body>
<div id="main-container">
<div id="message" class="myclass">Ready.</div>
<ul id="mapme">
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
</div>
<script src="bundle.js"></script>
</body>
</html>
要运行此示例,请创建main.js、index.html和template.html文件,然后在您的终端中...
npm i xstream @cycle/run @cycle/dom snabbdom-template brfs
browserify -t brfs main.js > bundle.js
open index.html
.
这是一个UIkit示例:
function main(sources) {
return {
DOM: xs.of([
['one', 'testing 1'],
['two', 'testing 2'],
['three', 'testing 3'],
['four', 'testing 4']
])
.map(function (list) {
return st(template, {
'#mapme': {_map: {'li': list.map(function (item, ii) {
const ret = {'h3.uk-accordion-title': item[0], 'div.uk-accordion-content p': item[1]}
if ( 0 === ii ) { ret['li'] = {class: 'uk-open'} }
return ret
})}}
})
})
}
}
给定template.html
<ul uk-accordion id="mapme">
<li>
<h3 class="uk-accordion-title">Item 1</h3>
<div class="uk-accordion-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</li>
<li>
<h3 class="uk-accordion-title">Item 2</h3>
<div class="uk-accordion-content">
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor reprehenderit.</p>
</div>
</li>
<li>
<h3 class="uk-accordion-title">Item 3</h3>
<div class="uk-accordion-content">
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat proident.</p>
</div>
</li>
</ul>
...产生类似的东西
<html>
<body>
<div id="main-container">
<ul uk-accordion id="mapme">
<li class="uk-open">
<h3 class="uk-accordion-title">one</h3>
<div class="uk-accordion-content">
<p>testing 1</p>
</div>
</li>
<li>
<h3 class="uk-accordion-title">two</h3>
<div class="uk-accordion-content">
<p>testing 2</p>
</div>
</li>
<li>
<h3 class="uk-accordion-title">three</h3>
<div class="uk-accordion-content">
<p>testing 3</p>
</div>
</li>
<li>
<h3 class="uk-accordion-title">four</h3>
<div class="uk-accordion-content">
<p>testing 4</p>
</div>
</li>
</ul>
</div>
<script src="bundle.js"></script>
</body>
</html>
哪个显示像
在此处尝试在线示例。