backbone.js getRegion和showChildView之间的区别

zpf6vheq  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(174)
const template1 = _.template('<h1>Marionette says hello!</h1>');
const template2 = _.template('<h1>Marionette is awesome!</h1>');
const myView1 = new Mn.View({template: template1});
const myView2 = new Mn.View({template: template2});
const MyView = Mn.View.extend({
  el: '#container',
  template: false,
  regions: {
    region1: '#region1',
    region2: '#region2'
  },
  onRender() {

    this.getRegion('region1').show(myView1);
    this.showChildView('region2', myView2); 

  }
});
const myView = new MyView();
myView.render();

getRegionshowChildView代码中的两个方法有什么区别?

fjnneemd

fjnneemd1#

这只是一个简化的语法,来自Marionette文档:

layoutView.getRegion('menu').show(new MenuView());

layoutView.getRegion('content').show(new MainContentView());

还有一些有用的快捷方式,可以使语法更简洁。

layoutView.showChildView('menu', new MenuView());

layoutView.showChildView('content', new MainContentView());

showChildView背后的代码

showChildView(name, view, ...args) {
    const region = this.getRegion(name);
    return region.show(view, ...args);
}

相关问题