0

是否可以将已经渲染的视图放入骨干木偶区域而不再次渲染?

例如:

region.show(myView); // This will call render on myView

我不希望该区域再次呈现我的视图。

如果我做:

region.attachView(myView); // This won't render myView, but it also won't show it
4

2 回答 2

2

首先覆盖木偶 ItemView 构造函数并像这样渲染(如果你想在 CollectionView 和 CompositeView 中使用它也覆盖它们的这些方法):

var fnCons = Marionette.ItemView.prototype.constructor,
    fnRender = Marionette.ItemView.prototype.render;
Marionette.ItemView.prototype.constructor = function(){
  this.firstRender = true;
  fnCons.apply(this,arguments);       
}

Marionette.ItemView.prototype.render = function(){
  this.firstRender = false;
  fnRender.apply(this,arguments);
}

然后只需覆盖默认区域#show,如果视图为view.firstRender真,则它尚未渲染并且必须渲染它,否则不需要任何操作。

Marionette.Region.prototype.show = function(view){    
  this.ensureEl();

  var isViewClosed = view.isClosed || _.isUndefined(view.$el);

  var isDifferentView = view !== this.currentView;

  if (isDifferentView) {
    this.close();
  }
  // These lines changed from default marionette action
  if(view.firstRender){
    view.render();
  }else{
    // Don anything else!
  }
  // End of overrided code


  if (isDifferentView || isViewClosed) {
    this.open(view);
  }

  this.currentView = view;

  Marionette.triggerMethod.call(this, "show", view);
  Marionette.triggerMethod.call(view, "show");
}

编辑

上面的解决方案工作正常,另一种方法是(这将假设视图已呈现):

Marionette.Region.prototype.showWithoutRender = function(view){
  this.ensureEl();

  var isViewClosed = view.isClosed || _.isUndefined(view.$el);
  var isDifferentView = view !== this.currentView;

  if (view !== this.currentView) {
    this.close();
  }

  if(isViewClosed || isDifferentView){
    this.open(view);
  }
  this.currentView = view;

  Marionette.triggerMethod.call(this, "show", view);
  Marionette.triggerMethod.call(view, "show");
}
于 2013-10-21T17:56:05.513 回答
1
// assume your view is already rendered
region.ensureEl();
region.open(myView);

不建议这样做,如果可以,请使用 region.show(myView)。因为它将关闭前一个视图以清理所有事件侦听器等...

于 2013-10-21T21:36:20.563 回答