2012-06-06 138 views
1

我有一个的statemachine,我使用了新的currentViewBinding换出整体containerView的部分,只要是使用currentViewBinding进入新状态:Ember.js - currentViewBinding并停止对每个视图过渡重新渲染

index: Ember.State.create({ 
    enter: function(manager) { 
     App.get('appController').set('feedView', Ember.View.create({ 
     templateName: 'dashboard_feed', 
     contentBinding: 'App.feedController.content', 
     controller: App.get('App.feedController') 
     })); 
    } 
    }) 

在这个时候,这些视图的渲染很慢。有没有办法让视图保留在内存中,并避免每次进入状态时重新渲染?

回答

3

我实际上为StackOverflow上的另一个问题提供了一个解决方案,但它在这里也超级相关。 Avoiding re-rendering of a flash object from scratch when view is reactivated

这里的的jsfiddle:http://jsfiddle.net/EE3B8/1

我有一个标志延伸ContainerView从破坏currentView其所在的破坏阻止它。你会想将视图实例存储在不会被销毁的地方。

App.ImmortalContainerView = Ember.ContainerView.extend({ 
    destroyCurrentView: true, 

    willDestroy: function() { 
     if (!this.destroyCurrentView) { this._currentViewWillChange(); } 
     this._super(); 
    } 
}); 

App.immortalView = Ember.View.create({ 
    template: Ember.Handlebars.compile(
     'I WILL LIVE FOREVER!' 
    ) 
}); 

+0

willDestroy被调用。我想知道你是否可以为我的场景提供任何指导。 我有一个包含侧边栏,顶栏和内容的容器视图,所有这些视图中的3个可以在1个状态转换中更改。 因此,当内容视图currentViewBinding更改时,整个containerview不会被销毁,因此willDestroy不会被调用。 我不想每次渲染容器视图的子视图,我只想隐藏并显示它。这可能吗? – dagda1

+0

我不认为这仍然有效。虽然第一个视图在内存中仍然存在,但在切换视图后,其状态为“销毁”,其isDestroyed属性为true,因此不会显示。 –

1

你可以扩展Ember.ContainerView显示/隐藏其currentView视图像这样:当整个containerview被破坏

App.FastContainerView = Ember.ContainerView.extend({ 
    toggleCurrentViewFast: true, 

    _currentViewWillChange: function() { 
    var childViews = this.get("childViews"), 
     currentView = this.get("currentView"); 

    if (this.toggleCurrentViewFast && childViews.indexOf(currentView) >= 0) { 
     currentView.set("isVisible", false); 
    } else { 
     this._super(); 
    } 
    }, 

    _currentViewDidChange: function() { 
    var childViews = this.get("childViews"), 
     currentView = this.get("currentView"); 

    if (this.toggleCurrentViewFast && childViews.indexOf(currentView) >= 0) { 
     currentView.set("isVisible", true); 
    } else { 
     this._super(); 
    } 
    } 
});