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

Introduce some template mechanism in the Javascript code #725

Open
marco-brandizi opened this issue Mar 10, 2023 · 0 comments
Open

Introduce some template mechanism in the Javascript code #725

marco-brandizi opened this issue Mar 10, 2023 · 0 comments
Labels
code quality project:client Related to the client/front-end war.

Comments

@marco-brandizi
Copy link
Member

Many Javascript functions in Knetminer renders big blocks of HTML, such as tables, by using HTML string embedded in the code and with a line-by-line approach.

For instance:

table = table + '<td><p id="evidence_exclude_' + ev_i + '" style="padding-right:10px;" class="excludeKeyword evidenceTableExcludeKeyword" title="Exclude term"></p>' +
  '<p id="evidence_add_' + ev_i + '" class="addKeyword evidenceTableAddKeyword" title="Add term"></p></td>';

This is dirty, poorly readable and hard to maintain. We should start migrating those functions that have a high number of lines like this towards something better, such as a template-like mechanism.

For instance, this is a good first example.

Some of the elements that improve this kind of code are:

  • avoid table += ..., even less table = table + .... Use multi-line strings, use per-row functions, including anonymous/lambda functions
  • avoid "<string>" + variable + "<string>". Use interpolation via ${variable}
  • Separate (as much as possible) HTML code from computations, using, among other things, interpolation, variables defined at the top of a rendering block, multi-line definitions in a file (for bigger blocks), lambdas.

Another approach is to load an empty HTML and then manipulate the DOM, as in here. The former seems simpler to me, but I'm open to discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality project:client Related to the client/front-end war.
Projects
None yet
Development

No branches or pull requests

1 participant