博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Cycle.js] Making our toy DOM Driver more flexible
阅读量:5244 次
发布时间:2019-06-14

本文共 1949 字,大约阅读时间需要 6 分钟。

Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent of the container element. We cannot yet create headers and inputs and all sorts of fancy DOM elements. In this lesson we will see how to send objects that describe what elements should exist, instead of strings as the DOM sink.

 

// Logic (functional)function main(sources) {  const click$ = sources.DOM;  const sinks = {    DOM: click$      .startWith(null)      .flatMapLatest(() =>         Rx.Observable.timer(0, 1000)         //describe what element should exist         .map(i => {            return {              tagName: 'h1',              children: [                {                  tagName: 'span',                  children: [                    `time esplsed: ${i}`                  ]                }              ]             }         })                 ),    Log: Rx.Observable.timer(0, 2000).map(i => 2*i),  };  return sinks;}// source: input (read) effects// sink: output (write) effects// Effects (imperative)function DOMDriver(obj$) {    function createElement(obj) {    const element = document.createElement(obj.tagName);    obj.children      .filter(c => typeof c === 'object')      // if it is object, then we need to create another element      .map(createElement)      .forEach(c => element.appendChild(c));        obj.children      .filter(c => typeof c === 'string')      .forEach(c => element.innerHTML += c);    return element;  }    obj$.subscribe(obj => {    const container = document.querySelector('#app');    container.innerHTML = '';    const element = createElement(obj);    container.appendChild(element);  });    const DOMSource = Rx.Observable.fromEvent(document, 'click');  return DOMSource;}function consoleLogDriver(msg$) {  msg$.subscribe(msg => console.log(msg));}const drivers = {  DOM: DOMDriver,  Log: consoleLogDriver,}Cycle.run(main, drivers);

 

转载于:https://www.cnblogs.com/Answer1215/p/5185665.html

你可能感兴趣的文章
C++中explicit的用法
查看>>
java 企业站源码 兼容手机平板PC 响应式 主流SSM框架 freemaker 静态引擎
查看>>
JS博客
查看>>
Docx转Doc操作(c#)
查看>>
一条简单的 SQL 执行超过 1000ms,纳尼?
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
Google Guava学习笔记——简介
查看>>
历时八年,HTML5 标准终于完工了
查看>>
17.树的子结构
查看>>
D - Mike and strings
查看>>
C++:多维数组的动态分配(new)和释放(delete)
查看>>
c#基础学习(0806)之抽象类实现多态
查看>>
S5PV210根文件系统的制作(一)
查看>>
51NOD 1244 莫比乌斯函数之和
查看>>
[bzoj1923]外星千足虫[高斯消元]
查看>>
centos下同时启动多个tomcat
查看>>