Giter Club home page Giter Club logo

cogizmo's Introduction

Cogizmo

Cogizmo is a convenience SuperClass for vanilla <custom-elements> (Web Component). This is primarily used to load external <template>, stylesheets and other assets so that your <custom-element> does not have to know the server filesystem architecture.

Usage

  1. Make a Custom Element class that extends Component

    MyComponent.js

    class MyComponent extends Cogizmo {
        // Component implementation details
    }
    
  2. Start the resource linking and component registration

    MyComponent.js

    MyComponent.manage();
    

Installation

Installation is very simple. Just place the required files in a web accessible directory.

  1. Install Cogizmo to a web accessible directory.

  2. Add Cogizmo to your document's <head> element.

    1. If you are using the module form (preferred), use:
      <script src="path/to/Cogizmo.mjs" type="module"></script>
      
    2. If you are using the non-module form, use:
      <script src="path/to/Cogizmo.js"></script>
      
  3. If using webcomponents polyfill, Cogizmo script must come after the polyfill.

For Compatibility

Some browsers do not have the complete WebComponents spec implemented. As a result, some browsers may require the webcomponents polyfills. Webcomponents polyfill is a smart polyfill that only includes features that are not yet implemented.

  1. Add <script src="path/to/webcomponents.js"></script> to document <head>

Features

External Templates

Using an external template file is as simple as placing a template.html file in your web component's root path. When MyComponent.manage() is called, it will automatically load the template and attach it to your class for attachment at construction time. You did remember to call super(), didn't you?

template.html

<template>
    <!-- Template details -->
</template>

External Stylesheet

External Stylesheets may be used by placing a <link> in your <template>. MyComponent.manage() auto-inspects the <template> and attaches the CSS to your class' internal template property.

template.html

<template>
    <link rel="stylesheet" href="path/to/stylesheet.css">
    <!-- Template details -->
</template>

Customizable <element-name>

Unforunately, the web component spec does not allow for multiple instances of the same ComponentClass to use the same <element-name>. This can be a problem as web components gain wider adoption. Future custom elements may extend, consume or otherwise depend upon earlier elements and version compatibility may become an issue if these code consumers rely on different versions of the same control. Cogizmo allows preemptive renaming of elements by use of a tagname custom attribute.

This requires no changes or conformance to your custom element's code.

application.html

<html>
<head>
    <script src="path/to/webcomponents-loader.js"></script>
    <script src="path/to/Cogizmo.js"></script>

    <script src="path/to/MyComponent.js" tagname="a-b-c"></script>
</head>
<body>
    <!-- Will render and work correctly. -->
    <a-b-c></a-b-c>

    <!-- Will no longer work. -->
    <my-component></my-component>
</body>
</html>

Custom Element Registration

Automatically calls customElements.define() for your <custom-element> utilizing the MyComponent.is property.

Web Component Compatibility

Cogizmo can be loaded as a module or non-module with ease. Module form is the preferred method. However, some browsers have not fully implemented ES Modules. Additionally, many custom elements may be written without the ability to digest modules. As a result, Cogizmo has a number of loading options to allow the greatest compatibility, forward and backward.

Browser Fallback Support

If you must support several browsing environments, it is typically better to add both the module and non-module form to your document.

<script src="path/to/Cogizmo.mjs" type="module"></script>
<script src="path/to/Cogizmo.js" nomodule></script>

This allows module enabled browsers to ignore the non-module, while out of date browsers will automatically ignore the module.

Supporting Non-module Custom Elements with Cogizmo Module

Even if very few or none of your custom elements are ES Modules, you can still use Cogizmo as a module. When loaded, Cogizmo looks for the use-global attribute on its script tag. This method does not conflict with fallback support.

<script src="path/to/Cogizmo.mjs" type="module" use-global></script>
<!-- Can still access Cogizmo from window/global object. -->
<script src="path/to/MyComponent.js"></script>

cogizmo's People

Contributors

fuzzicallogic avatar

Watchers

 avatar  avatar

Forkers

fuzzicallogic

cogizmo's Issues

Add Module

In the future, many custom elements will depend on the Cogizmo SuperClass. All of our elements do already. As we modularize our components, it would be nice to conform to SRP and encapsulate dependencies. The primary benefits of script modules are the ability to localize imports (reduce global namespace pollution) and one-time loads. If we modularize, then these imports can go directly into the component definition with no worries.

Add Self-Multiversioning

It is as important to support multiple versions of Cogizmo as it is the same custom elements. This is because each component will have its own versioning, and this may (will) lead to conflicting dependencies on Cogizmo.

Required for: Multiversion Support

Multiversion Support

The current Web Component spec does not allow for multiversion support, but this is a concern as some Web Components (and specifically Custom Elements) may depend upon other Web Components. While this concern is not immediate, given that web components are not widely adopted in sites and application, providing a simple mechanism may future-proof Web Components, in general.

*Requires: Customizable <element-name> and Customizable ClassName

Add Ready Promise

To better encourage asynchronous code, the API should be extended to include a function or property that returns a fulfilled or rejected Promise when Cogizmo has been initialized. To allow for functional localization, the fulfilled Promise should return the specific Superclass object.

Fire event on Ready

With the addition of async scripts, es modules and deferred scripts, it would probably be nice if a code consumers that are not importing could reliably know if/when Cogizmo is available. Such an event could allow for additional initialization paradigms, and localize the Superclass in non-module custom elements following functional philosophies.

Allow customizable Class names

The second major hurdle to overcome for multiversion support is based on the fact that you cannot use the same class object for more than one custom element definition. This means that exports and singly-instantiated scripts can only be defined once. The way around this is to allow a new ClassName prior to instantiating the component script. This will undoubtedly trade a minor amount of readability for the much greater return of being able to have multiple versions of the same element.

Required for: Multiversion Support
Related to: Customizable <element-name>

Allow customizable element names (Component.is)

One hurdle in multiversion support is that you cannot have multiple definitions for the same element. One of the core ideas of this micro-library is convenience. Adding the ability to redefine the element name prior to instantiating the component's <script> would be a huge step toward avoiding dependency collisions.

*Required for: #1 *

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.