2 Star 0 Fork 0

mirrors_WebReflection/hyperhtml-app

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
ISC

hyperhtml-app [deprecated]

This project has been improved, and replaced, by a-route.


Express like routing for pop and push states.

License: ISC Build Status donate

Designed for hyperHTML, it works as standalone module too.

V1 Breaking Changes

Path-To-RegExp breaks compatibility with Express <= 4.x.

  • RegExp special characters can only be used in a parameter
    • Express.js 4.x used all RegExp special characters regardless of position - this considered a bug
  • Parameters have suffixes that augment meaning - , + and ?. E.g. /:user
  • No wildcard asterisk () - use parameters instead ((.))

Minimalistic API

This project uses path-to-regexp module, the official Express JS path parser, and it combines it with an Express like API, mixing in the ease of page.js callbacks.

Based on the tiny, server side compatible, onpushstate module, this module has been created to be an ideal companion for the viperHTML tech fam.

API

  • app.get('/path/:user', callback) to react on paths and trap ctx.params such user or others.
  • app.use(path, callback) to add a generic middleware (similar to app.get but it accepts an array of paths)
  • app.delete(path, callback) to remove a specific callback
  • app.param(name, callback) to react once upfront whenever a specific parameter is passed along.
  • app.navigate(pathname[, options]) to push state and navigate to a different URL via standard History API. If the pathname and the search string are the same, it'll trigger a samestate type event (as opposite of popstate and pushstate). If there is a second options parameter and it has a replace or replaceState truthy property, it will not trigger a navigation event but it will replace the latest history with the current URL (shortcut for history.replaceState(...)).

Callbacks

Every callback will be invoked with a generic context object, a next function to invoke once everything is OK, and in case of app.param(name, fn) the value, as third argument, for the specified parameter.

const hyperApp = require('hyperhtml-app');

const app = hyperApp();

app.get('/', function (ctx) {
  console.log('Welcome');
});

app.use('/:user', function (ctx, next) {
  console.log(ctx.params);
});

app.param('user', function (ctx, next, name) {
  console.log(name); // hyper
});

app.navigate('/hyper');

Usage

hyperhtml-app lets you handle routing as you prefer, but the following is an example of how you might get started.

const hyperHTML = require('hyperhtml')
const HyperHTMLApp = require('hyperhtml-app')

/**
 * Utility
 */
function getCurrentRoute() {
  return window.location.pathname.replace('index.html', '');
}

/**
 * Router configuration
 */
const routerOutletEl = document.createElement('div');
const router = new HyperHTMLApp();
const renderRoute = hyperHTML.bind(routerOutletEl);

router.get('/user/:name', ctx => {
  const userName = ctx.params.name
  
  renderRoute`
    <h1>Hello ${userName}</h1>
    <button onclick=${() => router.navigate('/')}>
      Go back
    </button>
  `
  render()
});
router.get('/', () => {
  renderRoute`
    <h1>Hello world</h1>
    <button onclick=${() => router.navigate('/user/Julia')}>
      See Julia
    </button>
    <button onclick=${() => router.navigate('/user/Andrea')}>
      See Andrea
    </button>
  `
  render()
});

/**
 * Declarative render of the application and the active route
 */
const html = hyperHTML.bind(document.getElementById('root'))
function render() {
  return html`
    <div>
      ${routerOutletEl}
    </div>
  `;
}

// Trigger route handler for the initial route
router.navigate(getCurrentRoute());

Try it live

Compatibility

You can test this library live.

The only relatively modern features your target browser should support are both URL and History API.

You can use polyfill.io link in case you need these polyfills.

ISC License Copyright (c) 2017, Andrea Giammarchi, @WebReflection Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

简介

Express like routing for hyperHTML 展开 收起
README
ISC
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_WebReflection/hyperhtml-app.git
git@gitee.com:mirrors_WebReflection/hyperhtml-app.git
mirrors_WebReflection
hyperhtml-app
hyperhtml-app
master

搜索帮助