项目作者: callmecavs

项目描述 :
A Custom Element that AJAXs its content, style, and scripts.
高级语言: JavaScript
项目地址: git://github.com/callmecavs/ajax-component.git
创建时间: 2017-02-02T01:48:10Z
项目社区:https://github.com/callmecavs/ajax-component

开源协议:

下载


ajax-component

ajax-component on NPM Standard JavaScript Style

A Custom Element that AJAXs its content, style, and scripts.

Features

  • HTTP2 first. A component performs AJAX requests to fetch its HTML, CSS, and JS.
  • Scoped CSS. Via the Shadow DOM, ajax-component has native CSS scoping (no build step required).
  • Scoped JavaScript. Execute anything, with access to the global scope, but without polluting it.
  • Familiar. Same old HTML, CSS, and JS. Just new elements.

Install

  1. $ npm i ajax-component --save

Use

Decide on a name for your element (it must contain a -).

HTML

Using your name, add the new element to your markup:

  1. <!-- note that a closing tag is required -->
  2. <ajax-component></ajax-component>

Then, add the these attributes to load your resources:

Attribute Extension Required Optional
content .html
style .css
script .js
  1. <!-- will load HTML only -->
  2. <!-- while not technically required, scoped CSS and/or JS isn't useful without HTML -->
  3. <ajax-component content="path/to/file.html"></ajax-component>
  4. <!-- will load HTML content, scoped CSS, and scoped JS -->
  5. <ajax-component
  6. content="path/to/file.html"
  7. style="path/to/file.css"
  8. script="path/to/file.js">
  9. </ajax-component>

Finally, add the fetch attribute to your element.

It must have an initial value of false (or this wouldn’t be an AJAX component).

  1. <ajax-component
  2. fetch="false"
  3. content="path/to/file.html"
  4. style="path/to/file.css"
  5. script="path/to/file.js">
  6. </ajax-component>

JavaScript

Using your name, in your JavaScript, pass it to the import:

  1. // import the default export
  2. // this is a function that accepts an element name
  3. import createAJAXComponent from 'ajax-component'
  4. // call the imported function
  5. // pass the element name as used in the HTML to create the Custom Element
  6. createAJAXComponent('ajax-component')

An element will AJAX its resources when the fetch attribute changes to true.

  1. // selected the element we want to load
  2. const customEl = document.querySelector('ajax-component')
  3. // set the fetch attribute to true
  4. customEl.setAttribute('fetch', 'true')

Browser Support

pjax-component makes use of the Custom Elements v1 API. As such, it runs natively in the following (see caniuse):

  • Chrome 54+
  • Safari 10.1+
  • Opera 42+

Roadmap

  • Explore polyfill options (potential impact on scoping)

License

MIT. © 2017 Michael Cavalea