CycleJS - 订阅子组件的单击事件

时间:2016-03-16 20:31:22

标签: reactive-programming cyclejs

我是CycleJS的新手,我想订阅'点击'来自其父组件的子组件的事件;但是,它没有用。我能够在子组件内订阅事件。是否可以从其父组件订阅子组件的事件?如果可能,我该怎么办?这是父组件:

import Rx from 'rx';
import Cycle from '@cycle/core';
import CycleDOM from '@cycle/dom';
import isolate from '@cycle/isolate';
import _ from 'underscore';
import Inboxmails from './../components/inboxmails';

const {div} = CycleDOM;    
const Main =  (sources) => {

        const inboxmails=Inboxmails({DOM: sources.DOM});

        sources.DOM.select('#inbox_1')
                .events('click')
                .do(event => event.preventDefault())
                .subscribe(event => {
                    console.log(event);
                });


        const vtree$ = Rx.Observable.of(
                    div('.wrapper', [
                            inboxmails.DOM
                        ]));

            return {
                DOM: vtree$
            };
        };



    export default (sources) => isolate(Main)(sources);

这是子组件

import Rx from 'rx';
import Cycle from '@cycle/core';
import CycleDOM from '@cycle/dom';
import isolate from '@cycle/isolate';
const { div} = CycleDOM;

const Inboxmails = function (sources) {
    const inboxmails$ = Rx.Observable.of(div([
        div("#inbox_1",[
            "Click here"
        ])])
    );
    return {
        DOM: inboxmails$    
    };
};

export default (sources) => isolate(Inboxmails)(sources);

1 个答案:

答案 0 :(得分:4)

让孩子返回父母需要的事件汇。

const Inboxmails = function (sources) {
    const inboxmails$ = Rx.Observable.of(div([
        div("#inbox_1",[
            "Click here"
        ])])
    );
    return {
        DOM: inboxmails$,
        clicks: sources.DOM.select('#inbox_1').events('click')  
    };
};

然后父母可以使用inboxmails.clicks

但是,在Cycle.js中,代码中永远不应该有任何订阅(除非它是用于调试)。订阅电话应该只在驱动程序中。