Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

dart - Bootstrap.js not working in Polymer components

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...