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

Source: Internet
Author: User
Tags tagname

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

Code to be refactored:

functionMain (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). map (i =i),}; returnsinks;}

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:

functionh (TagName, children) {return{tagname:tagname, children:children}}//Logic (functional)functionMain (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). map (i =i),}; returnsinks;}

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

functionh (TagName, children) {return{tagname:tagname, children:children}}functionH1 (children) {return{tagName:' H1 ', Children:children}}functionspan (children) {return{tagName:' SPAN ', Children:children}}//Logic (functional)functionMain (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). map (i =i),}; returnsinks;}

The reason why we ' re introducing this function in the first place was 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 was a precursor to the 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 is this array With the span. Then we can also simplify this error function as that.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.