mamciek
mamciek

Reputation: 37

How to subscribe to events without DOM modifications in cyclejs (motorcyclejs)?

How can i subscribe events from some DOM nodes if I do not want to produce changes in DOM? How can I achive something like this in cyclejs or motorcyclejs?

function main({DOM}) {
   DOM.select('button').events('click').forEach(e => console.log(e))
   return {}
}

run(main, {DOM: makeDOMDriver('#app')})

updated: DOM tree already exits before running main function:

<div id="app">
  <button>Click</button>
</div>

The example above does not work, the event listener is not attached to DOM node.

Upvotes: 2

Views: 127

Answers (1)

cmdv
cmdv

Reputation: 1743

Here this should demonstrate what your setup should look like if you wanted the ability to view your DOM clicks:

const view = () => {
  return div('app',[
    button('Click')
  ]);
};

const intent = ({DOM}) => {
  const intent$ = DOM.select('button').events('click').map(e => console.log(e));
  return intent$;
};

const main = (sources) => {
  const intent$ = intent(sources);
  const view$ = Rx.Observable.just(view);
  return {
    DOM: view$

  };
};

run(main, {DOM: makeDOMDriver('#app')})

Upvotes: 0

Related Questions