Polymer / polymer

Our original Web Component library.
https://polymer-library.polymer-project.org/
BSD 3-Clause "New" or "Revised" License
22.05k stars 2.02k forks source link

How to share logic between polymer 1 componetns ! #5616

Closed pub-technology closed 4 years ago

pub-technology commented 4 years ago

Today, 2020 I'm working in a legacy system with POLYMER 1 (v.1.9.3)

This project created 4 years ago. I try to review codebase however I can't find any way to share logic between components in this project.

All Styles + HTML + Logic in 1 FILE. (Code between components seem duplicate everywhere.) In official document: We can share logic by using "BEHAVIOR"

Does that make sense if we have too more Behavior in our projects.

Expectation :

When I create a component I try with the structure below

Ex: User Component 
user-component.html: UI + Logic (properties, listeners...)
user-component-styles.html: styles
user-component-service.html (Define CRUD API) <- Use **Polymer Behavior**

Another Solution: As the traditional website - We create PURE JS File and import to Polymer component to use and share between components.

We can create a PURE Javascript file and import to use
Use this PURE JS file to share between 
components ( is it good or not )
web-padawan commented 4 years ago

Behaviors are the Polymer 1 way of sharing logic so you should use them. https://polymer-library.polymer-project.org/1.0/docs/devguide/behaviors

Regarding JS files: while you could technically try using native ES modules with polyfilled HTML Imports, this use case hasn't been considered at times when Polymer 1 has been built.

BTW you can use tools like jscpd to find duplicates in code.

pub-technology commented 4 years ago

Thanks for your advanced @web-padawan