Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating plugin options #5144

Merged
merged 1 commit into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ module.exports = function(Chart) {

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;
}
Expand Down
12 changes: 11 additions & 1 deletion src/core/core.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = {
* @private
*/
descriptors: function(chart) {
var cache = chart._plugins || (chart._plugins = {});
var cache = chart.$plugins || (chart.$plugins = {});
if (cache.id === this._cacheId) {
return cache.descriptors;
}
Expand Down Expand Up @@ -157,6 +157,16 @@ module.exports = {
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;
}
};

Expand Down
33 changes: 33 additions & 0 deletions test/specs/core.plugin.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,39 @@ describe('Chart.plugins', function() {

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'});
});
});
});