博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Cycle.js] Hyperscript as our alternative to template languages
阅读量:6803 次
发布时间:2019-06-26

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

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.

转载地址:http://pcjwl.baihongyu.com/

你可能感兴趣的文章
jquery文档处理如after错误
查看>>
P3564 [POI2014]BAR-Salad Bar
查看>>
js字符串与正则匹配
查看>>
2 变量、运算符、位运算
查看>>
电路的耦合方式
查看>>
JS 创建对象的7种方法(一)
查看>>
decode
查看>>
Python Socket套接字
查看>>
source from Other`s
查看>>
算法笔记--归并排序
查看>>
iOS --开发笔记:关于手机号码的判断【转】
查看>>
多标签分类
查看>>
Python基础教程(第2版 修订版) pdf
查看>>
python实现常见排序算法
查看>>
listctrl加入图标
查看>>
gem 更新源设置,ruby安装
查看>>
码农们:我们才是真正的土豪!
查看>>
[Node.js]NPM 使用
查看>>
Setup Factory打包winform程序
查看>>
window下php5.6-x64-ts可用php_redis.dll文件
查看>>