zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    HDU4652 Dice
    CF113D Museum / BZOJ3270 博物馆
    SHOI2013 超级跳马
    最基本的卷积与反演
    NOI2014 动物园题解
    SP11414 COT3
    new to do
    linux C++中宏定义的问题:error: unable to find string literal operator ‘operator""fmt’ with ‘const char [4]’, ‘long unsigned int’ arguments
    新装vs2010的问题:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    windows下删除虚拟串口的方法,以及解决串口使用中,无法变更设备串口号的问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5203727.html
Copyright © 2011-2022 走看看