Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

instantiated views referenced from inside an #if block only get properly destroyed the first time they are removed #565

Merged
merged 1 commit into from

2 participants

@burrows

When an already instantiated view is used inside of an #if block its destroy method is called the first time the condition becomes false. Then, when the condition becomes true again, the view is properly re-rendered, however when the condition becomes false once again, the nested view is removed from the document but its willDestroyLayer method is never invoked. I believe that this is happening because the nested view's isDestroyed property is set the first time it is torn down and then never reset.

A failing unit test to demonstrate the issue is attached.

@ColinCampbell ColinCampbell merged commit dea8a7a into from
@ColinCampbell

Merging this in, unsure what the fix is but thanks for the failing test!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 28, 2011
  1. @burrows

    Adds a failing unit test that demonstrates an issue with referencing an

    burrows authored
    instantiated view inside an #if helper.
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 0 deletions.
  1. +30 −0 frameworks/core_foundation/tests/views/template/handlebars.js
View
30 frameworks/core_foundation/tests/views/template/handlebars.js
@@ -60,6 +60,8 @@
*/
module("SC.TemplateView - handlebars integration");
+TemplateTests = {};
+
test("template view should call the function of the associated template", function() {
var view = SC.TemplateView.create({
templateName: 'test_template',
@@ -542,6 +544,34 @@ test("should update the block when object passed to #if helper changes and an in
});
});
+test("views nested within an #if helper should be destroyed every time they are removed", function() {
+ var view, didCreateLayerCalled = 0, willDestroyLayerCalled = 0;
+
+ TemplateTests.someView = SC.TemplateView.create({
+ template: SC.Handlebars.compile("<h1>hello</h1>"),
+ didCreateLayer: function() { didCreateLayerCalled++; },
+ willDestroyLayer: function() { willDestroyLayerCalled++; }
+ });
+
+ view = SC.TemplateView.create({
+ template: SC.Handlebars.compile('{{#if foo}}{{view TemplateTests.someView}}{{/if}}'),
+ foo: true
+ });
+
+ view.createLayer();
+
+ SC.run(function() { view.set('foo', false); });
+
+ equals(didCreateLayerCalled, 1, 'didCreateLayer should have been called once');
+ equals(willDestroyLayerCalled, 1, 'willDestroyLayer should have been called once');
+
+ SC.run(function() { view.set('foo', true); });
+ SC.run(function() { view.set('foo', false); });
+
+ equals(didCreateLayerCalled, 2, 'didCreateLayer should have been called twice');
+ equals(willDestroyLayerCalled, 2, 'willDestroyLayer should have been called twice');
+});
+
test("Should insert a localized string if the {{loc}} helper is used", function() {
SC.stringsFor('en', {
'Brazil': 'Brasilia'
Something went wrong with that request. Please try again.