Bootstrap.js not working in Polymer components

烈酒焚心 提交于 2019-11-28 07:04:20

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.

The problem as pointed out in the other answers is the shadow dom. Bootstrap does not see the elements in there, and thus can not do its magic. You can disable the shadowdom and apply the author styles like this:

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('main-messages')
class MainMessagesElement extends PolymerElement {
  MainMessagesElement.created() : super.created();

  //This enables the styling via bootstrap.css
  bool get applyAuthorStyles => true;
  //This enables the bootstrap javascript to see the elements
  @override
  Node shadowFromTemplate(Element template) {
    var dom = instanceTemplate(template);
    append(dom);
    shadowRootReady(this, template);
    return null; // no shadow here, it's all bright and shiny
  }   
}

You will need to remove the shadowdom for all parent elements as well.

WARNING: You will use the on-click event handler by using lightdom :(

Peter

I managed to get bootstrap.js working for my polymer components by adding the lightdom attribute to their definition.

<polymer-element name="my-element-with-bootstrap-js" lightdom 
apply-author-styles> 
<template>
<div>fancy template markup here</div>
</template> 
<script type="application/dart" src="my-element-with-bootstrap-js.dart"></script> 
</polymer-element>

And the host HTML could look like this:

<!DOCTYPE html>
<html>
<head>
<title>Unarin</title>
<meta name="viewport"
  content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />


<link rel="import" href="elements/my-element-with-bootstrap-js.html">

<script type="application/dart">export 'package:polymer/init.dart';</script>
<script src="packages/browser/dart.js" type="text/javascript"></script>

<link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<script
  src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"
  type="text/javascript"></script>
<script
  src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"
  type="text/javascript"></script>
</head>

<body>
  <my-element-with-bootstrap-js></my-element-with-bootstrap-js>
</body>
</html>

Please note: Using the lightdom attribute will replace your components from the shadow DOM to the light DOM, which can cause conflicts between your component's styling and the existing styles of the global context.

The related discussion: https://groups.google.com/a/dartlang.org/forum/#!topic/web-ui/ps6b9wlg1Vk

Based on the discussion I wasn't a 100% sure if the lightdom modifier will stay the standard way to achieve this kind of functionality for polymer components. Perhaps the authors could comment on that?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!