Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cycle-director

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cycle-director

A router driver for cycle.js based on director

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Cycle-Director

This is a first attempt a making a router driver for cycle.js using director

Install

npm install cycle-director

Client Example

import {run, Rx} from '@cycle/core';
import {h, makeDOMDriver} from '@cycle/dom';

import {makeClientDriver} from 'cycle-director';

let author = () => { return "author" };
let books = () => { return "books"};
let viewBook = (id) => {return "viewBook: bookId is populated: " + id};
let viewChapter = (bookId, chapterNumber) => { return "BookId: " + bookId + " Chapter: " + chapterNumber}

let routes = [
  { url: "/author",
    on: author,
    after: () => {if (!confirm("You sure you want to leave this page?")) {
      window.location.hash = '#/author'
    }}
  },
  { url: "/books", on: [books, () => { return "An inline route handler"}]},
  { url: "/books/view/:bookId", on: viewBook},
  { url: "/books/view/:bookId/chapter/:chapterNumber", on: viewChapter }
]

function render(text) {
  return h('div', [
    h('ul', [
      h('li', [h('a', {href: '#/author'}, 'Author')]),
      h('li', [h('a', {href: '#/books'}, 'Books')]),
      h('li', [h('a', {href: '#/books/view/33'}, 'Book 33')]),
      h('li', [h('a', {href: '#/books/view/33/chapter/2'}, 'Book 33 Chapter 2')])
    ]),
    h ('h1', text)
  ])
}

function main({DOM, Router}){
  let route$ = Rx.Observable.from(routes);

  let view$ = Router
    .map((output) => {
      console.log("Output: " + output);
      return render(output);
    })

  return {
    DOM: view$,
    Router: route$
  };
}


let drivers = {
  DOM: makeDOMDriver('.app'),
  Router: makeRouterDriver({
    html5history: false,
    notfound: () => { return 'Page can not be found'}
  })
};

run(main, drivers);

API

makeRouterDriver(options)

Arguments

options - options are all configuration options supported by director

Return

(Function) The Router Driver function. It expects an Observable of Route Objects as input, and outputs the path of the current route.

Route Object

  • url (required): path to mount routing events
  • optionally any routing event director supports

Server Side Example

Uses cycle-http-server

import {run} from '@cycle/core';
import {makeServerDriver} from 'cycle-http-server';
import {makeHTTPDriver} from 'cycle-director';

function helloWorld() {
  this.res.writeHead(200, {'Content-Type': 'text/plain'});
  this.res.end("Hello, world!");
}

let routes = {
  "/hello": {
    get: helloWorld
  }
};

function main({Server, Router}) {

  Server.subscribe( ({req, res}) => {
    Router.dispatch(req, res, (err) => {
      if (err) {
        res.writeHead(404);
        res.end(err.toString());
      }

  Router.get("/bonjour", helloWorld);
  Router.get("/hola/", helloWorld);

  Server.listen(3000);
}

let drivers = {
  // Takes routes and [configuration](https://github.com/flatiron/director#configuration)
  Router: makeHTTPDriver(routes, {
    strict: false
  }),
  Server: makeServerDriver()
}

run(main,drivers);

Keywords

FAQs

Package last updated on 26 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc