Javascript 木偶事件
木偶3.2.0版 onAttach生命周期事件未触发 据 标有“*”的事件仅在区域el为时触发 附加到DOM 猫大师解释为什么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
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
方法。谢谢您的解释