Currently in our main() function,  we get click$ event.

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

What if we want to change it to mouse event? we need to somehow modiy the DOMSource it return from:

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

So for main() function, we need to call a function able to manage the tagName and eventType:

const mouseover$ = sources.DOM.selectEvents('span', 'mouseover');

And for the DomDirver, we need add a function which enable user to pass down element and eventType:

  const DOMSource = {
selectEvents: function(tagName, eventName){
return Rx.Observable.fromEvent(document, eventName);
}
};

-------------------------

Code:

// Logic (functional)
function main(sources) {
const mouseover$ = sources.DOM.selectEvents('span', 'mouseover');
const sinks = {
DOM: mouseover$
.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 = {
selectEvents: function(tagName, eventName){
return Rx.Observable.fromEvent(document, eventName);
}
};
return DOMSource;
} function consoleLogDriver(msg$) {
msg$.subscribe(msg => console.log(msg));
} const drivers = {
DOM: DOMDriver,
Log: consoleLogDriver,
} Cycle.run(main, drivers);

[Cycle.js] Fine-grained control over the DOM Source的更多相关文章

  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. RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?

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

  4. 学习RxJS:Cycle.js

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

  5. [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 ...

  6. [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 ...

  7. js 控制展开折叠 div html dom

    js 控制展开折叠 div    html dom <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...

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

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

  9. 【转】从Vue.js源码看异步更新DOM策略及nextTick

    在使用vue.js的时候,有时候因为一些特定的业务场景,不得不去操作DOM,比如这样: <template> <div> <div ref="test" ...

随机推荐

  1. 利用ESLint检查代码质量

    1. ESLint ESLint 是一个插件化的 javascript 代码检测工具,它可以用于检查常见的 JavaScript 代码错误,也可以进行代码风格检查,这样我们就可以根据自己的喜好指定一套 ...

  2. Abstract-抽象类

    本人理论较差,之前会做却不明原因,最近在改别人的代码发现实现方式完全不同,但对于我这个理论白痴来说完全不知道为什么别人要这么写,好处在哪里. 没有理论的指导,会用也只是不断的Copy前人,永远无法让程 ...

  3. django: template using & debug

    模板的作用方法有如下三种: blog/views.py: from django.template import loader, Context, Template from django.http ...

  4. OC基础 代理和协议

    OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...

  5. ul li span addClass removeClass

    <link type="text/css" href="./style/css/base.css" rel="stylesheet"& ...

  6. Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)

    原文:InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序) InstallShield Limited Edi ...

  7. const的重载

    class A { private: int a; public: A(int x) :a(x){}//构造函数并不能重载 void display(){ cout << "no ...

  8. DX笔记之六------游戏画面绘图之透明特效的制作方法

    原文链接:http://blog.csdn.net/zhmxy555/article/details/7338082 透明效果 由于所有的图文件都是以矩形来储存的,我们也许会需要把一张怪兽图片贴到窗口 ...

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

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

  10. js判断当前操作系统

    function validataOS(){ if(navigator.userAgent.indexOf(“Window”)>0){ return ”Windows”; }else if(na ...