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);

[Cycle.js] Making our toy DOM Driver more flexible的更多相关文章

  1. [Cycle.js] From toy DOM Driver to real DOM Driver

    This lessons shows how we are able to easily swap our toy DOM Driver with the actual Cycle.js DOM Dr ...

  2. [Cycle.js] Read effects from the DOM: click events

    So far we only had effects that write something to the external world, we are not yet reading anythi ...

  3. [Cycle.js] Hello World in Cycle.js

    Now you should have a good idea what Cycle.run does, and what the DOM Driver is. In this lesson, we ...

  4. 学习RxJS:Cycle.js

    原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...

  5. RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?

    RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...

  6. [Cycle.js] The Cycle.js principle: separating logic from effects

    The guiding principle in Cycle.js is we want to separate logic from effects. This first part here wa ...

  7. jquery.cycle.js简单用法实例

    样式: a{text-decoration: none;} *{;;} /*容器设置*/ .player { width:216px; height:248px; background:url(htt ...

  8. js动态监听dom变化

    原生js 动态监听dom变化,根据不同的类型绑定不同的处理逻辑 // Firefox和Chrome早期版本中带有前缀   var MutationObserver = window.MutationO ...

  9. jquery.cycle.js

    jquery.cycle.js简单用法实例 样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width ...

随机推荐

  1. Node.js 实现简单小说爬虫

    最近因为剧荒,老大追了爱奇艺的一部网剧,由丁墨的同名小说<美人为馅>改编,目前已经放出两季,虽然整部剧槽点满满,但是老大看得不亦乐乎,并且在看完第二季之后跟我要小说资源,直接要奔原著去看结 ...

  2. NHIBERNATE之映射文件配置说明(转载4)

    二十.自定义值类型   开发者创建属于他们自己的值类型也是很容易的.比如说,你可能希望持久化Int64类型的属性, 持久化成为VARCHAR 字段.NHibernate没有内置这样一种类型.自定义类型 ...

  3. 打开较大存储量的.sql文件时,出现SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问

    1. “消息 15281,级别 16,状态 1,过程 xp_cmdshell,第 1 行SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访 ...

  4. .net Signalr 使用笔记

    官方参考地址:http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host 1.服务器端可以是控制台.winfor ...

  5. Android 虚拟机快捷键

    何须这样麻烦,直接用快捷键就行了,你用鼠标点来点去怎么还不如用快捷键来得快:一下是快捷键: Home键(小房子键) 在键盘上映射的就是home键,这倒是很好记. Menu键 用于打开菜单的按键,在键盘 ...

  6. nginx_http核心模块(二)

    对一些常用的配置项做一些解释:详细请看官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html 1. alias Syntax: ali ...

  7. oracle数据库使用plsql(64位)时出现的问题

    64位win7上装PL/SQL,经常会遇见“Could not load "……\bin\oci.dll"”这个错误,我查了一下资料,原因是PL/SQL只对32位OS进行支持,解决 ...

  8. discuz3.2x增加邮箱验证功能

    为防止垃圾用户多次注册,为disczu增加邮箱验证功能. 大致分为二步: 1.申请邮箱,这里推荐使用腾讯免费企业邮箱:https://exmail.qq.com/portal/introducefre ...

  9. DataTables给表格绑定事件

    $(document).ready(function() { $('#example').dataTable(); $('#example tbody').on('click', 'tr', func ...

  10. [原创]零基础R语言教程---第一课---认识R语言

    教程的录制的确是折腾了一番,一连录了二十多遍,有时候激动的说错了字,有时候不知道下一句说啥.. 不过好在第一课已经搞定了,哈哈. 虽然内容现在看起来还有点简单, 不过牛b也是一个过程嘛. 我会坚持下去 ...