javascript - Patterns for avoiding jQuery silent fails -


Is there any good practice to prevent your jQuery code from failing silently?

For example:

  $ ('. It's #: my (complex selector)'). DoSomething ();  

I know that every time this line is executed, the intention of the selector is to match at least one element or some attributes of the elements. Is there a standard or good way to validate it?

I thought of something like this:

  var $ matchedElements = $ ('. This #: my (complexSelector)'); If not ($ matched elements.line and lieutenant; 1) do not throw 'any matching element'; $ MatchedElements.doSomething ();  

Apart from this, I think the unit will be a valid option instead of messing up the test code.

My question may be silly, but I wonder what is the better option than what I am currently doing or not, except maybe I am doing wrong investigation, if any element is mine Whether or it does not match the selector However, as the page grows, selectors can stop matching certain elements, and the pieces of functionality may stop working unknowingly.

You can write a plugin:

  jQuery.fn NeededElements = function (amount, exact match) {if (amount === undefined) {amount = 1; }; If (this.length & lt; zodiac || this.length & gt; zodiac & amp; & amp; amp; perfectMatch) {throw new error (this.length "elements instead of" (exactMatch "at least":? " ") + Zodiac); }; This return; };  

then:

  $ ('your selector'). Requirements (2) .bind ('click', function () {});  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -