首页 > Web开发 > 详细

[Cycle.js] From toy DOM Driver to real DOM Driver

时间:2016-02-24 09:18:03      阅读:317      评论:0      收藏:0      [点我收藏+]

This lessons shows how we are able to easily swap our toy DOM Driver with the actual Cycle.js DOM Driver, a more solid, more flexible, more efficient implementation.

 

<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
<script src="https://rawgit.com/cyclejs/cycle-core/v6.0.3/dist/cycle.js"></script>
<script src="https://rawgit.com/cyclejs/cycle-dom/v9.1.0/dist/cycle-dom.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>  
</head>
<body>
  <div id="app"></div>
</body>
</html>

 

const {h, h1, span, makeDOMDriver} = CycleDOM;

// Logic (functional)
function main(sources) {
  const mouseover$ = sources.DOM.select(‘span‘).events(‘mouseover‘);
  const sinks = {
    DOM: mouseover$
      .startWith(null)
      .flatMapLatest(() => 
        Rx.Observable.timer(0, 1000)
         .map(i => 
           h1({style: {background: ‘red‘}}, [
             span([
               `Seconds elapsed: ${i}`
             ])
           ])
         )           
      ),
    Log: Rx.Observable.timer(0, 2000).map(i => 2*i),
  };
  return sinks;
}

// Effects (imperative)
function consoleLogDriver(msg$) {
  msg$.subscribe(msg => console.log(msg));
}

const drivers = {
  DOM: makeDOMDriver(‘#app‘),
  Log: consoleLogDriver,
}

Cycle.run(main, drivers);

 

[Cycle.js] From toy DOM Driver to real DOM Driver

原文:http://www.cnblogs.com/Answer1215/p/5211692.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!