From 33e50ffe6fae62310043812abc86c66ef360ec68 Mon Sep 17 00:00:00 2001 From: Tom Densham Date: Tue, 19 Nov 2013 15:24:48 +0000 Subject: [PATCH] FIX: Ensure that ChangeTrackerOptions doesn't get overriden From @hafriedlander: Hi. Sorry, I was going to have a look at this on the back of that issue @chillu raised but you beat me to it. There's a couple of edge cases that aren't obvious that come from ChangeTrackerOptions being an object, and might need an Entwine API extension to fix nicely. Objects in entwine properties are a bit dangerous, because javascript always passes them by reference instead of cloning them. Entwine also doesn't clone them when using them as default values. The result is that this patch will repeatedly add that selector to the result every time getChangeTrackerOptions is called, so it'll be there once the first time it's called, twice the second, etc. The right fix at the moment would look like: ```php $('.cms-edit-form').entwine({ getChangeTrackerOptions: function() { // Figure out if we're still returning the default value var isDefault = (this.entwineData('ChangeTrackerOptions') === undefined); // Get the current options var opts = this._super(); if (isDefault) { // If it is the default then... // clone the object (so we don't modify the original), var opts = $.extend({}, opts); // modify it, opts.ignoreFieldSelector +=', input[name=IsSubsite]'; // then set the clone as the value on this element // (so next call to this method gets this same clone) this.setChangeTrackerOptions(opts); } return opts; }); ``` This is super ugly though, non-obvious, and could maybe be handled better in the entwine layer. See https://github.com/silverstripe/silverstripe-subsites/pull/125 --- javascript/LeftAndMain_Subsites.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/javascript/LeftAndMain_Subsites.js b/javascript/LeftAndMain_Subsites.js index 8988eb6..805a434 100644 --- a/javascript/LeftAndMain_Subsites.js +++ b/javascript/LeftAndMain_Subsites.js @@ -106,8 +106,27 @@ }); $('.cms-edit-form').entwine({ + /** + * TODO: Fix with Entwine API extension. See https://github.com/silverstripe/silverstripe-subsites/pull/125 + */ getChangeTrackerOptions: function() { - this.ChangeTrackerOptions.ignoreFieldSelector+=', input[name=IsSubsite]'; + // Figure out if we're still returning the default value + var isDefault = (this.entwineData('ChangeTrackerOptions') === undefined); + // Get the current options + var opts = this._super(); + + if (isDefault) { + // If it is the default then... + // clone the object (so we don't modify the original), + var opts = $.extend({}, opts); + // modify it, + opts.ignoreFieldSelector +=', input[name=IsSubsite]'; + // then set the clone as the value on this element + // (so next call to this method gets this same clone) + this.setChangeTrackerOptions(opts); + } + + return opts; } });