[Cycle,js] Customizing effects from the main function

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Cycle,js] Customizing effects from the main function相关的知识,希望对你有一定的参考价值。

How can we show one string on the DOM, and a completely different string on Console log? This lesson shows how we can make our main function return multiple Observables, each one targeted at a different type of effect.

 

// Logic (functional)
function main() {
  return {
    DOM: Rx.Observable.timer(0, 1000).map( i => `timer is ${i}`),
    Log: Rx.Observable.timer(0, 1000).map(i => 2*i )
  };
i}


function DOMEffect(text$) {
  text$.subscribe(text => {
    const container = document.querySelector(‘#app‘);
    container.textContent = text;
  });
}

function consoleLogEffect(msg$) {
  msg$.subscribe(msg => console.log(msg));
}

const sinks = main();
DOMEffect(sinks.DOM);
consoleLogEffect(sinks.Log);
  

 

以上是关于[Cycle,js] Customizing effects from the main function的主要内容,如果未能解决你的问题,请参考以下文章

[Cycle.js] Hello World in Cycle.js

[Cycle.js] The Cycle.js principle: separating logic from effects

学习RxJS:Cycle.js

学习RxJS:Cycle.js

替换 cycle2.js 中的 <img> 标签

在 Cycle.js 中呈现所有 dom 后运行 js 代码