Quelqu'un peut-il m'expliquer pourquoi il y a 2 inclusions de jQuery dans le kit de démarrage Emberjs

StackOverflow https://stackoverflow.com/questions/9522695

  •  15-11-2019
  •  | 
  •  

Question

Je viens de regarder l'index.html fourni avec le kit de démarrage Emberjs que vous obtenez en cliquant sur "Télécharger le kit de démarrage" sur http://emberjs.com/

Il y a un peu d'étrange du code à la fin de la balise du corps (des scripts de chargement au dernier moment):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script>
<script src="js/libs/ember-0.9.5.min.js"></script>
<script src="js/app.js"></script>

OK Je comprends les deux derniers, chargez la bibliothèque Ember, puis exécutez l'application Ember.Je comprends aussi le premier, obtenez la jQuery du CDN de Google, mais je ne comprends pas pourquoi vous auriez jamais la deuxième étiquette de script!Est-ce juste pour que vous ne dépendez pas de CDN de Google et de livrer votre propre copie de JQuery?

Quelqu'un peut-il me dire s'il s'agit de la meilleure pratique ou si je devais déposer un bogue avec Emberjs afin qu'ils suppriment cela de leur kit de démarrage.

merci

Était-ce utile?

La solution

I think the author is thinking that in case jquery is unable to load from the googleapis then to be safe include a local version. Notice the

!window.jQuery

Autres conseils

If jQuery is loaded from the first script tag, then window.jQuery would evaluate to true. Thus, !window.jQuery would evaluate to false, and the expression in the second line would short circuit. However, if jQuery did not load from the first line, then !window.jQuery will be true, and the document will write the script tag for a privately hosted version. This allows the client to load jQuery from Google APIs, which will probably be faster, but gives them a fallback in case the load fails.

You might want to read up on type coercion in JavaScript.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top