解决getRegion和showChildView之间的差异

时间:2016-11-15 17:29:06

标签: javascript backbone.js marionette

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中的两种方法之间存在哪些差异?

1 个答案:

答案 0 :(得分:4)

这只是Marionette doc

的快捷语法
layoutView.getRegion('menu').show(new MenuView());

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

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

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

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

code behind showChildView

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