How to render multiple instances of the same view in a handlebars template

I am attempting to render multiple instances of a containerView in a single handlebars template.

http://jsfiddle.net/skane/bZFB3/4/

<script type='text/x-handlebars'>
  {{ view "App.MyContainerView" }}
</script>

The above code works correctly as long as I don't add additional {{ view }} tags referencing the same View. I don't understand why... I have attached a complete fiddle that demonstrates the problem. (check the console to see the error that is thrown).

The error that is thrown is "something you did caused a view to re-render after it was rendered but before it was inserted into the DOM".

Any help/clarification would be appreciated!

Steve


The problem is that you're create()ing the child views in your container view definition, so the same child view instances are being (or attempting to be) inserted twice, one for each instance of the container view. Change those create()s to extend() and you'll be fine.

Properties set as part of an extend() block are on the prototype, so they are shared between instances. In the case of a container view, the container view will instantiate a class if it finds one where it expects to find a child view, and thus have its own instance of that child view, but if an instance is already present on the prototype, it will attempt to use that, and blow up since another instance of the containerview thinks it owns that child view instance and has already inserted it.

链接地址: http://www.djcxy.com/p/60814.html

上一篇: 当视图位于“preRender”中时,会触发emberjs didInsertElement

下一篇: 如何在句柄模板中呈现同一视图的多个实例