缓存木偶视图的最佳做法

时间:2023-01-20 22:35:00

In the application we are developing, we have a CollectionView whose every ItemView contains a link to the item-details page. Also, every ItemView contains a checkbox, because items can be selected in the CollectionView to perform bulk actions on them.

在我们正在开发的应用程序中,我们有一个CollectionView,其每个ItemView都包含指向item-details页面的链接。此外,每个ItemView都包含一个复选框,因为可以在CollectionView中选择项目以对它们执行批量操作。

When switching to the ItemDetails view, we want to keep the state of the CollectionView, ideally without having to redraw it (a bit like GMail when switching from inbox to mail and back). Our solution is to render the two views in two different regions and to hide one when switching from one to the other.

当切换到ItemDetails视图时,我们希望保持CollectionView的状态,理想情况下无需重绘(当从收件箱切换到邮件和返回时有点像GMail)。我们的解决方案是在两个不同的区域中渲染两个视图,并在从一个视图切换到另一个视图时隐藏一个视图。

My perplexity about this solution is that

我对这个解决方案的困惑在于

  • Marionette doesn't seem to be meant for this kind of use.
  • 木偶似乎不适合这种用途。
  • It is not very memory-friendly, since all the DOM elements are never deleted.
  • 它不是非常友好的,因为从不删除所有DOM元素。

Is there any better solution to achieve this goal? Storing the state somewhere, close the CollectionView and redraw it later is another possible solution, but would it imply a heavy computation overhead? (we are quite scared about redrawing views).

有没有更好的解决方案来实现这一目标?将状态存储在某个地方,关闭CollectionView并在以后重绘它是另一种可能的解决方案,但它是否意味着繁重的计算开销? (我们非常害怕重绘视图)。

2 个解决方案

#1


4  

Marionette allows for showing a view in a region without closing the view that was already rendered. You simply pass in {preventClose: true} to the show() method of the region. You would still need to maintain a reference to the collection view though so you can later re-show it or close it yourself.

Marionette允许在不关闭已经渲染的视图的情况下在区域中显示视图。您只需将{preventClose:true}传递给该区域的show()方法即可。您仍然需要维护对集合视图的引用,以便稍后可以重新显示或自行关闭它。

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#showing-a-view

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#showing-a-view

#2


0  

Unless your collection is very large, there's not a problem with just rerendering the collectionView when switching back from a itemDetail view. You do need to store the state of the checkboxes indeed.

除非你的集合非常大,否则从itemDetail视图切换回来时重新渲染collectionView没有问题。您确实需要存储复选框的状态。

However, I don't see what's really wrong with your other approach. It's probably even faster and there's nothing wrong with just hiding one region and showing another. If that works for you, go ahead.

但是,我没有看到你的其他方法出了什么问题。它可能更快,只是隐藏一个区域并显示另一个区域没有任何问题。如果这对您有用,请继续。

Regarding the memory issue, as long as you're looking at the collection or itemDetail there isn't much to gain by closing either one of the views (especially if your itemDetail views are not very large). Once you move away from that section (thus not looking at the collection or itemDetail view anymore) you could just close the layout that contains these two regions. That'll free up any memory used by these regions.

关于内存问题,只要你查看集合或itemDetail就可以通过关闭其中一个视图获得很多东西(特别是如果你的itemDetail视图不是很大)。一旦您离开该部分(因此不再查看集合或itemDetail视图),您可以关闭包含这两个区域的布局。这将释放这些地区使用的任何内存。

#1


4  

Marionette allows for showing a view in a region without closing the view that was already rendered. You simply pass in {preventClose: true} to the show() method of the region. You would still need to maintain a reference to the collection view though so you can later re-show it or close it yourself.

Marionette允许在不关闭已经渲染的视图的情况下在区域中显示视图。您只需将{preventClose:true}传递给该区域的show()方法即可。您仍然需要维护对集合视图的引用,以便稍后可以重新显示或自行关闭它。

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#showing-a-view

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#showing-a-view

#2


0  

Unless your collection is very large, there's not a problem with just rerendering the collectionView when switching back from a itemDetail view. You do need to store the state of the checkboxes indeed.

除非你的集合非常大,否则从itemDetail视图切换回来时重新渲染collectionView没有问题。您确实需要存储复选框的状态。

However, I don't see what's really wrong with your other approach. It's probably even faster and there's nothing wrong with just hiding one region and showing another. If that works for you, go ahead.

但是,我没有看到你的其他方法出了什么问题。它可能更快,只是隐藏一个区域并显示另一个区域没有任何问题。如果这对您有用,请继续。

Regarding the memory issue, as long as you're looking at the collection or itemDetail there isn't much to gain by closing either one of the views (especially if your itemDetail views are not very large). Once you move away from that section (thus not looking at the collection or itemDetail view anymore) you could just close the layout that contains these two regions. That'll free up any memory used by these regions.

关于内存问题,只要你查看集合或itemDetail就可以通过关闭其中一个视图获得很多东西(特别是如果你的itemDetail视图不是很大)。一旦您离开该部分(因此不再查看集合或itemDetail视图),您可以关闭包含这两个区域的布局。这将释放这些地区使用的任何内存。