Usually we use template languages like Handlebars, JSX, and Jade to create. One simple way we can create our own template language is to write a function that returns these objects for us. This lessons shows how we can use these functions as a DSL to create our DOM description objects.

Code to be refactored:

function main(sources) {
const mouseover$ = sources.DOM.selectEvents('span', 'mouseover');
const sinks = {
DOM: mouseover$
.startWith(null)
.flatMapLatest(() =>
Rx.Observable.timer(0, 1000)
.map(i => {
return {
tagName: 'H1',
children: [
{
tagName: 'SPAN',
children: [
`Seconds elapsed: ${i}`
]
}
]
};
})
),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
return sinks;
}

Inside map, return a large object which represent html element.

We can create a function which accept 'tagName'and 'children', to simply our main function:

function h(tagName, children) {

  return {
tagName: tagName,
children: children
}
} // Logic (functional)
function main(sources) {
const mouseover$ = sources.DOM.selectEvents('span', 'mouseover');
const sinks = {
DOM: mouseover$
.startWith(null)
.flatMapLatest(() =>
Rx.Observable.timer(0, 1000)
.map(i => h('H1', [
h('SPAN', [
`Seconds elapsed: ${i}`
])
])
)
),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
return sinks;
}

Also we can create function for each tagName to even simply our code:

function h(tagName, children) {

  return {
tagName: tagName,
children: children
}
} function h1(children){ return {
tagName: 'H1',
children: children
}
} function span(children){
return {
tagName: 'SPAN',
children: children
}
} // Logic (functional)
function main(sources) {
const mouseover$ = sources.DOM.selectEvents('span', 'mouseover');
const sinks = {
DOM: mouseover$
.startWith(null)
.flatMapLatest(() =>
Rx.Observable.timer(0, 1000)
.map(i => h1([
span([
`Seconds elapsed: ${i}`
])
])
)
),
Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
};
return sinks;
}

The reason why we're introducing this function in the first place is that it looks really similar to what exists in cycle-dom, and it's a function called hyperscript. That's where the H comes from. That's our alternative to a template language.

This is a precursor to what we're going to see in cycle-dom. We're going to replace this outermost object, as well, with return H of H1 as a tag name, and the children are this array with the span. Then we can also simplify this error function like that.

[Cycle.js] Hyperscript as our alternative to template languages的更多相关文章

  1. 学习RxJS:Cycle.js

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

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

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

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

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

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

  6. jquery.cycle.js

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

  7. jquery图片切换插件jquery.cycle.js参数详解

    转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...

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

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

  9. jQuery图片切换插件jquery.cycle.js

    Cycle是一个很棒的jQuery图片切换插件,提供了非常好的功能来帮助大家更简单的使用插件的幻灯功能 下载cycle插件并引入,此时,注意把引入它的代码放在引入jQuery主文件之后. <he ...

随机推荐

  1. Linux是如何启动的

    今天早上在上操作系统课的时候,老师有提到计算机从按下开关键到最后由操作系统全然接管的整个过程. 只是讲课毕竟是十分抽象的,由于之前自己也看过这方面的内容,可是老是记不住,所以今天晚上就花了点时间,把& ...

  2. storm启动流程

    email:chenguibin2004@126.com storm: 是一个分布式的实时流式计算框架,具有低延迟.高可用.分布式.可扩展.数据不丢失的特点, storm包含四个核心组件: Nimbu ...

  3. Internetmap.apk实现原理分析

    1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...

  4. 解决vsftpd 530 Permission denied报错

    虚拟机装好RedHat后,准备使用filezilla连接,输入IP地址,root用户,密码,快速连接,报错: 故障排除: 1.首先检查系统是否开启了vsftp服务,如果没有开启,先开启该服务. 方法1 ...

  5. python-socket 粘包问题

    解决粘包的问题: 1.服务端在发送数据之前,先把发送数据的长度告诉客户端,要发送多少数据,然后客户端根据这个数据的长度循环接收就OK 传输过程: 服务端:     1.send  #数据长度     ...

  6. php不会的点

    1.DIRECTORY_SEPARATOR:DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量,不需要任何定义与包含即可直接使用 ...

  7. 前端--关于HTML

    在讲HTML之前不得不先简单粗略提一下浏览器以及浏览器与HTML的关系.众所周知,浏览器就是一个应用程序,这个应用程序可以完成网络调用.展示接收的html文档等.严格来讲HTML文档就是按照某个规则写 ...

  8. VC中遍历目标进程中的模块

    VC中遍历目标进程中的模块 MFC代码win32 也可以用 在下面代码进行修改转换就可以了CString strModule; 可以换成 char* 但是MODULEENTRY32结构中的szModu ...

  9. 关于WinForm/Web如何使用缓存Cach

    原文链接:http://www.cnblogs.com/zfanlong1314/archive/2013/03/28/2986403.html Cache 的绝对到期与滑动到期 绝对到期:设置绝对过 ...

  10. volatile举列说明const

    1.即使本程序中虽然不改变这种类型的值,但别的比如中断程序可能会改变这个值,加上volatile,编译器不优化,每次都重新访问这个值做判断 2.如 unsigned char flag = 1; in ...