Marionette bubble event from itemview to parent layoutview?

前端 未结 5 1653
面向向阳花
面向向阳花 2021-02-08 17:10

I have a layout view with a region, in that region I have a item view that triggers an event but it doesn\'t seem to be bubbled up to the layout view. Am I doing something wron

5条回答
  •  盖世英雄少女心
    2021-02-08 18:13

    Triggers don't quite work like that: your layout is using them wrong. Triggers are a convenience to raise an event signal given a certain interaction (e.g. a click).

    What you want is to use triggerMethod (https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.functions.md#marionettetriggermethod) to trigger a function in your layout. See http://jsfiddle.net/ZxEa5/ Basically, you want this in your show function:

    childView.on("btn:clicked", function(){
      layout.triggerMethod("childView:btn:clicked"); 
    });
    

    And in your layout:

    onChildViewBtnClicked: function(){
       https://leanpub.com/marionette-gentle-introduction
    });
    

    Event bubbling only happens automagically with collection?composite views because they're tightly associated with their item views. If you want a layout to monitor one of its child views, you need to set that up on your own.

    Shameless plug: if you want to learn more about how to structure and clean up your code with Marionette, you can check out my book (https://leanpub.com/marionette-gentle-introduction) where this type of concept (and its applications) is explained in more detail.

提交回复
热议问题