Skip to content
This repository has been archived by the owner on Dec 2, 2017. It is now read-only.

yuheiy/page-dispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Page Dispatcher

Divide logic of JavaScript by your page.

Concept

Details are here (Japanese)

Installation

$ npm install --save @yuheiy/page-dispatcher

Usage

'use strict';

var PageDispatcher = require('@yuheiy/page-dispatcher');
var dispatcher = new PageDispatcher();

// run on all pages
console.log('common');

dispatcher.on('home', function() {
  // run only on home
  console.log('home');
});

var currentPageType = document.body.dataset.pageType;
dispatcher.run(currentPageType);
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Home - Site Name</title>
    <script src="bundle.js" defer></script>
  </head>

  <body data-page-type="home">
    <h1>Home</h1>
  </body>
</html>

Another way

'use strict';

var PageDispatcher = require('@yuheiy/page-dispatcher');
var dispatcher = new PageDispatcher();

dispatcher.on('common', function() {
  // run on all pages
  console.log('common');
});

dispatcher.on('home', function(message) {
  // run only on home
  console.log(message);
});

window.run = dispatcher.run.bind(dispatcher);
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Home - Site Name</title>
  </head>

  <body>
    <h1>Home</h1>

    <script src="bundle.js"></script>
    <script>
      run('common');
      run('home', 'This is a home of this site.');
    </script>
  </body>
</html>

About

Divide logic of JavaScript by your page.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published