Permalink
Browse files

Controller update to help clean up old record arrays and objects.

With tests.
  • Loading branch information...
1 parent 051ccf2 commit 6f25c15a9b54bfd2203636c15159e89af06ee7ab @geoffreyd geoffreyd committed with wagenet May 21, 2011
@@ -39,6 +39,26 @@ SC.Controller = SC.Object.extend(
@property {Boolean}
*/
- isEditable: YES
+ isEditable: YES,
+
+ /**
+ * Set this to YES if you are setting the controller content to a recordArray
+ * or other content that needs to be cleaned up (with `.destroy()`) when
+ * new content is set.
+ */
+ destroyContentOnReplace: NO,
+
+ contentObjectDidChanged: function() {
+ var oldContent, newContent;
+
+ if (!this.get('destroyContentOnReplace')) return;
+
+ oldContent = this._oldContent,
+ newContent = this.get('content');
+ if (oldContent && newContent !== oldContent && oldContent.destroy) {
+ oldContent.destroy();
+ }
+ this._oldContent = newContent;
+ }.observes('content')
});
@@ -0,0 +1,59 @@
+// ==========================================================================
+// Project: SproutCore - JavaScript Application Framework
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+
+var content, newContent, controller, destroyCount;
+
+// ..........................................................
+// SINGLE OBSERVABLE OBJECT
+//
+
+SC.TestObject = SC.Object.extend();
+
+SC.TestObject.reopen({
+ destroy: function() {
+ destroyCount = 1;
+ }
+});
+
+module("SC.ObjectController - content destroyed", {
+ setup: function() {
+ content = SC.TestObject.create({
+ foo: "foo1", bar: "bar1"
+ });
+ newContent = SC.Object.create({
+ foo: "foo2"
+ });
+ destroyCount = 0;
+ console.log('SSSSS');
+ controller = SC.ObjectController.create({
+ destroyContentOnReplace: YES,
+ content: content
+ });
+ },
+
+ teardown: function() {
+ controller.destroy();
+ }
+});
+
+test("Setting content should call 'destroy' on old content if destroyContentOnReplace has been set", function() {
+ controller.set('content', newContent);
+ equals(destroyCount, 1, 'destroyCount');
+ equals(controller.getPath('content.foo'), 'foo2');
+});
+
+test("Setting content should NOT call 'destroy' on old content if destroyContentOnReplace has not been set", function() {
+ controller.set('destroyContentOnReplace', NO);
+ controller.set('content', newContent);
+ equals(destroyCount, 0, 'destroyCount');
+ equals(controller.getPath('content.foo'), 'foo2');
+});
+
+test("Setting content should NOT call 'destroy' if set to the same object", function() {
+ controller.set('content', content);
+ equals(destroyCount, 0, 'destroyCount');
+});

0 comments on commit 6f25c15

Please sign in to comment.