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. Qt使用QGraphicsView实现滑动窗体效果

    QGraphicsView用来显示一个滚动视图区的QGraphicsScene内容.QGraphicsScene提供了QGraphicsItem的容器功能.通常与QGraphicsView一起使用来描 ...

  2. React 入门最好的实例-TodoList

    React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...

  3. 配置linux中文

    1.~/.bash_profile文件添加一下内容并执行source  ~/.bash_profile export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK 2./etc ...

  4. 获取Excel表中各个Sheet的方法

    获取Excel表中各个Sheet的方法 private void simpleButton2_Click(object sender, EventArgs e) { OfdBOM.Filter = & ...

  5. Linq to Entities不识别方法

    db.UserValidates.Include(a => a.User).Where(uv => u.UserValidates.Contains(uv, c)).ToList(); 执 ...

  6. trailingZeroes

    Given an integer n, return the number of trailing zeroes in n!. 给一个数字n,返回它n!数字后面有多少个0. public class ...

  7. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  8. mongodb3 权限认证问题总结

    mongodb3 权限认证问题总结 标签(空格分隔): mongodb 权限 数据库 认证 ubuntu用户安装最新版本mongodb 添加key sudo apt-key adv --keyserv ...

  9. 【4】python核心编程 第七章-映射和集合类型

    1.映射类型的相关函数 函数 操作 dict([container]) 创建字典的工厂函数.如果提供了容器类(container) , 就 用其中的条目填充字典,否则就创建一个空字典. len(map ...

  10. NET站点Web部署

    NET站点Web部署(一键发布的实现) 在开发过程中经常需要发布到开发环境.测试环境或者预发布环境上给其他同事进行测试验证效果等等,每次发布都要备份,拷贝,修改配置文件等等重复操作非常的麻烦,效率大打 ...