Browse files

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 #125
  • Loading branch information...
1 parent d21881d commit 33e50ffe6fae62310043812abc86c66ef360ec68 @nedmas nedmas committed with nedmas Nov 19, 2013
Showing with 20 additions and 1 deletion.
  1. +20 −1 javascript/LeftAndMain_Subsites.js
View
21 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;
}
});

0 comments on commit 33e50ff

Please sign in to comment.