gpt4 book ai didi

javascript - Backbone Marionette 区域显示无渲染

转载 作者:行者123 更新时间:2023-11-28 20:13:51 24 4
gpt4 key购买 nike

是否可以将已渲染的 View 放入主干 Marionette 区域而不再次渲染?

例如:

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

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

如果我这样做:

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

最佳答案

首先重写 marionette 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);
}

然后只需覆盖默认的region#show,如果 View 为view.firstRender为true,则它尚未渲染并且必须渲染它,否则不需要任何操作。

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");
}

编辑

上面的解决方案工作正常,另一种方法是(这将假设 View 已渲染):

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");
}

关于javascript - Backbone Marionette 区域显示无渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19496732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com