GithubHelp home page GithubHelp logo

jhipstercn / router-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jianliaoim/router-view

0.0 1.0 0.0 483 KB

Router as a View for React

Home Page: http://router-view.mvc-works.org/

CoffeeScript 96.40% CSS 3.60%

router-view's Introduction

Router View for React

Location bar is a view of store

This project is based on react, immutable and actions-recorder.

Demo http://router-view.mvc-works.org/

Initial idea

Router is part of MVC and we can certainly divide it into M, C and V for better managements. It's like react-redux-router but designed based on actions-recorder.

Ideas behind it: Router is a View

Usage

npm i --save router-view

M part, add initial router object in store:

Addressbar = require 'router-view'
pathUtil = require 'router-view/lib/path'

rules = pathUtil.expandRoutes [
  ['home', '/']
  ['demo', '/demo']
  ['skip', '/skip/~']
  ['team', '/team/:teamId']
  ['room', '/team/:teamId/room/:roomId']
  ['404', '~']
]

oldAddress = "#{location.pathname}#{location.search}" # for history API
# oldAddress = location.hash.substr(1) # for hash
router = pathUtil.getCurrentInfo rules, oldAddress
store = store.set 'router', router

"V" part, mount Addressbar component to manipulate History API:

Addressbar
  route: store.get('router')
  rules: routes
  onPopstate: (info, event) ->
    # figure out the new router object and dispatch a `router/go` action
  inHash: false # fallback to hash from history API
  skipRendering: false # true to allow model/view inconsistency during loading

"C" part, connect action to store:

switch actionType
  when 'router/go' # <--- bind this action
    router.go store, actionData
  else
    console.warn ":Unknown action type: #{actionType}"
    store

Read src/ for details.

DSL

~ refers to "any path" in this library.

And in store the route information (info) is like:

name: 'room'
data:
  teamId: '12'
  roomId: '34'
query:
  isPrivate: 'true'

Parameters and querystrings are supported. Get this from store and render the page.

Notice

  • keep in mind that router-view is totally based on immutable-js.
  • if you need to route asynchronously, try set skipRendering to true during loading
  • undefined value is eliminated on purpose, fire an issue if you think differenly.

Theme

http://archwall.xyz/wp-content/uploads/2015/09/skyscrapers-city-sleeps-blue-ocean-skyscrapers-sky-aerial-skyline-beautiful-evening-streets-buildings-lights-traffic-night-shore-free-wallpapers.jpg

License

MIT

router-view's People

Contributors

tiye avatar

Watchers

 avatar

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.