viernes, 20 de enero de 2017

ToopTip el nombre de la función es el mismo en jqueru-ui y bootstrap. Error por colisión de nombres



Using JQuery UI and Bootstrap togheter in the same web page


In the past few months we’ve seen the Bootstrap framework became more and more popular among web developers. The reasons are fairly obvious, we’re talking  about a powerful CSS/JS enhancement library featuring a lot of useful stuff to build a responsive, mobile-friendly webpage fully compatible with every major browser/device. The most recent version of the framework (v3.3.1 by the time of this post) contains a lot of useful javascript plugin requiring JQuery v1.9.0 or higher in order to work: there’s no doubt that some of these websites are also running JQuery UI.
These 2 frameworks are indeed able to work togheter, but we need to pay attention to at least two naming conflicts between them which need to be fixed in order to avoid JS errors and, most importantly, get them to work properly. We’re talking about the tooltip and buttonplugins, which happen to exist in both of these libraries with the exact same name.

The Issue.

Let’s take the tooltip plugin as an example. Open the address http://jqueryui.com/tooltip/ and look at the sample code – by clicking on the “view source” link – to see how the JQuery UI tooltipplugin actually works:
The highlighted text shows a call to the tooltip function which, thanks to JQuery UI, is added at runtime to any JQuery object. Bootstrap does the exact same job, as shown in the official tutorial:
This nasty naming conflict prevents the plugins to work an might also generates some Javascript errors depending on the framework <script> references order inside the web page. For example, here’s what you’ll most likely get in your Javascript Console if Boostrap is inserted after JQuery UI and the latter’s tooltip plugin is being used:
Uncaught TypeError: Cannot read property ‘documentElement’ of null
We get this message because the tooltip plugin provided by JQueryUI has a completely different – and hardly compatible – interface than the Bootstrap plugin sharing its name.

 The Solution.

The best way to make the 2 libraries blend togheter and “constructively” fix the aforementioned naming conflicts is to use the not-so-well-known $.widget.bridge plugin, which allows us to re-define each previously assigned prototype function name. The plugin is already included inside the JQuery UI standard distribution package, so we won’t add any additional stuff to our website. The usage is quite simple:
These commands need to be executed after the JQueryUI and before the Bootstrap js file references. In the following example we can see a full implementation example in the <head> block of a common webpage:
After you do this, the tooltip and button prototype methods will be univocally routed to the Bootstrap ones: those provided by JQuery UI will also be usable as long as we call them by using the new alias we gave them: uitooltip and uibutton.

Fuente: http://www.ryadel.com/en/using-jquery-ui-bootstrap-togheter-web-page/

No hay comentarios:

Publicar un comentario

Jesús Moreno - Ingeniero Ténico Informático - consultor Informático

Hola, soy Jesús Moreno Ingeniero Técnico Informático en sistemas por la US y propietario de éste blog. Mi trabajo en los ultimos años se ...