[Cycle.js] Hyperscript as our alternative to template languages

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.

posted @   Zhentiw  阅读(368)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示