Bootstrap.js不能在Polymer组件中工作 [英] Bootstrap.js not working in Polymer components

查看:196
本文介绍了Bootstrap.js不能在Polymer组件中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将目前的Dart Web UI项目翻译为Polymer.dart。我们使用Bootstrap 3作为前端样式和许多网络动画(例如下拉菜单,模态,工具提示)。



然而,在我将一个Web UI组件翻译成Polymer之后,所有的Bootstrap JavaScript停止为子组件工作,因为它现在封装在ShadowDOM中。要从Dart访问ShadowDOM中的元素,我必须使用shadowRoot字段,并且从Chrome中的JavaScript我必须使用webkitShadowRoot属性,但仍然无法获得单个下拉菜单正常工作。



首先,在bootstrap中调用下拉('toggle')API之后,下拉菜单会出现,但不会消失。此外,似乎不可能检测到哪个链接由点击事件触发,因为事件在窗口级别上触发。这意味着我找不到要显示哪个下拉菜单。



有人有使用twitter-bootstrap和Polymer的经验吗?

$ b $

b

正如你所指出的,问题出在Bootstrap的JS。它不知道ShadowDOM并尝试使用全局选择器从DOM获取节点。例如,在bootstrap-carousel.js中,我看到了以下内容:

  $ .on('click.carousel.data-api',...',function(e){...}); 

我们稍后在Polymer中使用Bootstrap组件进行了探讨:
https://github.com/Polymer/more-elements/tree/stable/Bootstrap



最有趣的是< bs-accordion-item> 演示



这个过程基本上是将自定义元素包装在自定义元素内部并调用其API。



对于CSS 包括他们的样式表在你的元素,事情应该一般工作:

 < polymer-element name = my-element> 
< template>
< link rel =stylesheethref =bootstrap.css>
...
< / template>
< script> ...< / script>
< / polymer-element>

请记住,如果bootstrap.css来自CDN,则需要启用CORS以使polyfills正常工作。当本地HTML导入到达时,此要求消失。


I'm currently working on translating our current Dart Web UI project to Polymer.dart. We use Bootstrap 3 for the front-end styling and many web animations (e.g. dropdown menus, modals, tooltips).

However after I translated one Web UI component into Polymer, all the Bootstrap JavaScript stopped working for the sub component, since it was now encapsulated in the ShadowDOM. To access an element in the ShadowDOM from Dart I have to use the shadowRoot field, and from JavaScript in Chrome I have to use the webkitShadowRoot property, but still I can't get a single dropdown menu to work properly.

Firstly, after calling the dropdown('toggle') API in bootstrap, the dropdown menu appears but never goes away. Also, it seems impossible to detect which link is triggered by the click event since the event is fired on the window level. This means I can't find which dropdown menu to display.

Does anyone have experience using twitter-bootstrap and Polymer together?

解决方案

Your questison is about the Polymer.dart port, but applies to Polymer.js devs as well :)

As you point out, the issue is with Bootstrap's JS. It doesn't know about ShadowDOM and attempts to fetch nodes from the DOM using global selectors. For example, in bootstrap-carousel.js, I see things like:

$(document).on('click.carousel.data-api', ...', function (e) { ...});

We explored using Bootstrap components inside of Polymer a little bit ago: https://github.com/Polymer/more-elements/tree/stable/Bootstrap

The most interesting to you would be <bs-accordion-item> (Demo)

The process is basically to wrap Bootstrap stuff inside a custom element and call into their APIs.

For the CSS: if you include their stylesheet inside your element, things should generally work:

<polymer-element name="my-element">
  <template>
    <link rel="stylesheet" href="bootstrap.css">
    ...
  </template>
  <script>...</script>
</polymer-element>

Keep in mind that if bootstrap.css is coming from a CDN, it needs to be CORS-enabled for the polyfills to work properly. This requirement goes away when native HTML Imports lands.

这篇关于Bootstrap.js不能在Polymer组件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆