首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CycleJS -订阅子组件的单击事件

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

Stack Overflow用户
提问于 2016-03-16 20:31:21
回答 1查看 282关注 0票数 0

我是CycleJS新手,我想从其父组件订阅子组件的“单击”事件;但是,它不起作用。我能够订阅子组件中的事件。是否可以从其父组件订阅子组件的事件?如果有可能,我该怎么做?下面是父组件:

代码语言:javascript
复制
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);

这是子组件

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-18 07:49:34

让子节点返回父级所需事件的接收器。

代码语言:javascript
复制
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中,代码中不应该有任何订阅(除非是用于调试)。订阅呼叫应该只在驱动程序中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36046340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档