Is Conditional Visualization possible with Pure JS?

I use Pure JS directives for rendering:

http://beebole.com/pure/documentation/rendering-with-directives/

If there is no node in the template, the default behavior of Pure JS crashes due to:

node "XXX" not found in the template

This default behavior is completely understandable, as it ensures that there are no inconsistencies in the template. In the same cases, however, I would like to skip the unsuccessful assignment and continue to work with the rest of the assignments (perhaps register an error) in order to avoid the failure of the entire rendering due to one error / typo.

Is there any way to get this behavior with Pure JS? Can I tell Pure JS to make the element simply "if it exists"?

+4
source share
1 answer

Unfortunately, the current stable version of Pure JS (version: 2.79) doesn’t allow the "if it exists" element to be displayed?

The following Pure code snippet shows how the error occurs:

if(selector === '.' || ( !selector && attr ) ){ target[0] = dom; }else{ target = plugins.find(dom, selector); } if(!target || target.length === 0){ return error('The node "' + sel + '" was not found in the template:\n' + outerHTML(dom).replace(/\t/g,' ')); } 

As you can see, if target not found (one of the libraries, such as jQuery, dojo, etc.), then the specified error is thrown.


The workaround that I use in such situations is as follows:

  • I use a template with all the possible elements so that Pure does not produce errors.
  • Some elements (e.g. displaying error messages, etc.) are hidden using CSS classes.
  • I am changing CSS classes using Pure and JavaScript functions with directives to hide / show elements depending on the input.
+3
source

Source: https://habr.com/ru/post/1392088/


All Articles