Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 木偶事件_Javascript_Marionette - Fatal编程技术网

Javascript 木偶事件

Javascript 木偶事件,javascript,marionette,Javascript,Marionette,木偶3.2.0版 onAttach生命周期事件未触发 据 标有“*”的事件仅在区域el为时触发 附加到DOM 猫大师解释为什么 Mn.View.extend({ tagName: 'table', className: 'table', template: _.template(template), regions: { body: { el: 'tbody', replaceElement: tru

木偶3.2.0版

onAttach生命周期事件未触发

标有“*”的事件仅在区域el为时触发 附加到DOM

猫大师解释为什么

Mn.View.extend({
    tagName: 'table',
    className: 'table',
    template: _.template(template),
    regions: {
        body: {
            el: 'tbody',
            replaceElement: true
        }
    },
    initialize(options) {
      console.log(Mn.isNodeAttached(this.el)); // false
      setTimeout(() => console.log(Mn.isNodeAttached(this.el)), 0); // true
    },
    serializeData() {
      return {
        foo: 'bar'
      }
    },
    onRender() {
      this.showChildView('body', new TableBodyView());
    },

    onAttach() {
      // why onAttach not work ?
      console.log('attached');
    }

  });

不清楚您是如何实例化视图并将其附加到页面的,但只有在将视图“显示”到区域中时才会触发
onAttach
方法。因此,例如,如果手动渲染视图并将其附加到DOM,则不会触发该视图

下面的代码段显示了一个示例,
View
onAttach
方法将触发:

const App = Mn.Application.extend({
    region: '.content',
    onStart: function() {
        this.showView(new View());
    }
});

new App().start();
小提琴:


请注意,这并不一定需要显示在应用程序的区域中——当显示视图时,也会触发
onAttach
方法。

谢谢您的解释