RiotJS - How to pass events between subtags using Observable pattern?

前端 未结 5 586
暗喜
暗喜 2021-01-01 02:15

Im not really sure if Im understanding correctly the way observables work and how to get references from mounted tags. I have a component. Within this comp

5条回答
  •  自闭症患者
    2021-01-01 02:58

    Try to pass a shared observable to both tags.

    var sharedObservable = riot.observable();
    
    riot.mount('search', {observable: sharedObservable}); // the second argument will be used as opts
    riot.mount('collection', {observable: sharedObservable});
    

    And then in the tags, just use it:

    this.opts.observable.trigger('myEvent');
    
    this.opts.observable.on('myEvent', function() { ... });
    

    EDIT: Or even better, since your search and collection tags are child tags of another riot tag (page) (and thus you also don't need to mount them manually), you can use the parent as the shared observable. So just trigger or handle events in your child tags like this:

    this.parent.trigger('myEvent');
    
    this.parent.on('myEvent', function() { ... });
    

提交回复
热议问题