Fixed dataview filtering breaking, after compressing JS with e.g. Google Closure Compiler #243
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
We're using SlickGrid (it's awesome), and recently updated to latest. We found that after running our build scripts, which compress our JS files using Google Closure Compiler, our grids that used DataView filtering all broke.
Cause
The problem is with the general approach that SlickGrid.DataView's
compileFilter()
andcompileFilterWithCaching()
use; that is, usingFunction.toString()
to fetch original source code forfnTemplate
and the user's filter function, to generate a new function.The code relies on carefully named variables to act as targets for later string replacement. These will be shortened/changed by compressors, so the behavior ends up broken.
(The code also makes some pretty strong expectations about the style/structure of the user's filter function, which causes other issues... I will elaborate in a separate issue)
Fix
To fix, I rewrote the function templates as strings, since it's only there so that the function body can be retrieved via
Function.toString()
. This prevents them from being modified by a JS compressor. Unfortunately, the readability is penalised a little bit.Furthermore, there isn't any workaround offered by Google Closure Compiler; there is no way to ignore or "not-compress" the code, whether at the function level or at the file level.
I hope this saves someone else hours of debugging...