updateConfig(me);
+ // plugins options references might have change, let's invalidate the cache
+ // https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
+ plugins._invalidate(me);
+
if (plugins.notify(me, 'beforeUpdate') === false) {
return;
}
* @private
*/
descriptors: function(chart) {
- var cache = chart._plugins || (chart._plugins = {});
+ var cache = chart.$plugins || (chart.$plugins = {});
if (cache.id === this._cacheId) {
return cache.descriptors;
}
cache.descriptors = descriptors;
cache.id = this._cacheId;
return descriptors;
+ },
+
+ /**
+ * Invalidates cache for the given chart: descriptors hold a reference on plugin option,
+ * but in some cases, this reference can be changed by the user when updating options.
+ * https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
+ * @private
+ */
+ _invalidate: function(chart) {
+ delete chart.$plugins;
}
};
expect(plugin.hook).toHaveBeenCalled();
expect(plugin.hook.calls.first().args[1]).toEqual({a: 'foobar'});
+
+ delete Chart.defaults.global.plugins.a;
+ });
+
+ // https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
+ it('should invalidate cache when update plugin options', function() {
+ var plugin = {id: 'a', hook: function() {}};
+ var chart = window.acquireChart({
+ plugins: [plugin],
+ options: {
+ plugins: {
+ a: {
+ foo: 'foo'
+ }
+ }
+ },
+ });
+
+ spyOn(plugin, 'hook');
+
+ Chart.plugins.notify(chart, 'hook');
+
+ expect(plugin.hook).toHaveBeenCalled();
+ expect(plugin.hook.calls.first().args[1]).toEqual({foo: 'foo'});
+
+ chart.options.plugins.a = {bar: 'bar'};
+ chart.update();
+
+ plugin.hook.calls.reset();
+ Chart.plugins.notify(chart, 'hook');
+
+ expect(plugin.hook).toHaveBeenCalled();
+ expect(plugin.hook.calls.first().args[1]).toEqual({bar: 'bar'});
});
});
});