Giter Site home page Giter Site logo

polymer-classes-in-es6's Introduction

This is quick experiment to try creating a custom element class using ES6's class syntax. The Polymer team has already signaled an interest in supporting this ability someday; this repo simply explores the extent to which that's possible today.

This experiment relies on Polymer 1.x and the Traceur transpiler.

Demo

A simple demo shows a "Hello, world" element created with Polymer as an ES6 class. Requires Google Chrome.

Thoughts

  • Creating an element class with class feels good!
class GreetElement extends PolymerElement {
};
  • In an effort to make element definition as DRY as possible, by default an element class with the Pascal case name FooElement will result in a custom element whose tag name is the hypenated, lowercase name foo-element. If a custom tag name is desired, override the is getter:
class BoringElement extends PolymerElement {
  get is() {
    return "awesome-element";
  }
}
  • It'd be nice if classes could be auto-registered. ES6 classes don't appear to support a static constructor/initialization function, so it's unclear how this could be achieved. The demo shows use of a static method called register:
GreetElement.register();

While it's too bad this explicit registration step is required — there's little point in creating a custom element class unless you're going to register it — at least this feels cleaner and more DRY than

document.registerElement( "my-element", MyElement );

polymer-classes-in-es6's People

Stargazers

Safi avatar M Haidar Hanif avatar Frédéric Mascaro avatar Kristian Mandrup avatar Leif Olsen avatar  avatar David Steinkopff avatar Saneyuki Tadokoro avatar Erik Isaksen avatar

Watchers

Jan Miksovsky avatar Bede Overend avatar Erik Isaksen avatar James Cloos avatar

polymer-classes-in-es6's Issues

Setup doesn't work with dom-module

Hey @JanMiksovsky, I couldn't get this setup to actually work with Polymer's registration system. Namely, it doesn't respect the dom-module for the element or create shady dom.

<dom-module id="my-element">
  <template>
    <span>my element</span>
  </template>
</dom-module>

class PolymerElement extends Polymer.Class({}) {
  get is() { return 'my-element'; }
}

class MyElement extends PolymerElement {
  created() {
    this.textContent = "Hello, " + this.textContent + "!";
  }
}

document.registerElement('my-element', MyElement);

<my-element>eric</my-element>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.