-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
This is a more declarative way to check if the context is a jQuery el… #3303
Conversation
…ement. Also no need to check .getContext twice.
@@ -13,7 +13,7 @@ module.exports = function() { | |||
}; | |||
|
|||
// Support a jQuery'd canvas element | |||
if (context.length && context[0].getContext) { | |||
if (typeof(jQuery) === 'function' && context instanceof jQuery) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this continue to work even if jQuery is not in the global namespace? I think that case could happen in a commonJS style build.
I also think we should, if possible, add a test for this
I'm not sure we should make a special case for jQuery. Actually, I already changed this lookup in the cleanup I'm doing around the canvas but also added support for a string id. Will be similar to this: var Chart = function(element, config) {
// ...
if (typeof(element) === 'string') {
element = document.getElementById(element);
}
if (element.length) {
element = element[0];
}
if (element instanceof HTMLCanvasElement) {
element = element.getContext('2d');
}
if (!element instanceof CanvasRenderingContext2D) {
// ERROR
return;
}
context = element;
// ... This is in anticipation of the new way to create a chart via a div element. What do you think? |
@simonbrunel This updated code is clear. However I still think a check for jquery could be used. Checking for a length property can work for a lot of edge cases. @etimberg This should work even if jquery isn't defined globally. This is a short-circuiting operation. |
@etimberg I can add an explicit test case if you think this would be acceptable. |
That's exactly the point: handle more cases and not target jQuery specifically. If it's an array (or compatible object), simply deals with the first element. Saves some code and a special case. |
I agree with @simonbrunel that if we can have a more generic check (for a bunch of different cases) then we should do that. I would definitely like a test either way though |
I already have these changes locally, with unit tests but still need some work until being able to submit a PR (since it's part of a more important refactor). |
Closing, this functionality was added in #3474 |
Please consider the following before submitting a pull request:
Guidelines for contributing: https://github.com/chartjs/Chart.js/blob/master/CONTRIBUTING.md
Example of changes on an interactive website such as the following:
…ement.
Also no need to check .getContext twice.