Skip to content

Commit

Permalink
new release which removes option 'defaultTarget' from BrowserRouter a…
Browse files Browse the repository at this point in the history
…nd instead allows to, optionally, pass a custom 'send' function in the options
  • Loading branch information
Roger Padilla authored and Roger Padilla committed Oct 28, 2017
1 parent 623fea4 commit a74a855
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 181 deletions.
60 changes: 32 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ So basically, you give prouter a set of path expressions and a callback function
## Why prouter?
- **Unobtrusive:** it is designed from the beginning to play well with vanilla JS or with any library/framework out there: [Polymer](https://www.polymer-project.org/1.0/), [React](http://facebook.github.io/react/), [Handlebars](http://handlebarsjs.com/), etc.
- **Learn once and reuse it** Express.js is very well known and used across the world, why not bringing the same API (wherever possible) to the browser? Under the hood, prouter uses the same (wonderful) library than express for parsing URLs [Path-to-RegExp](https://github.com/pillarjs/path-to-regexp).
- **Really lightweight:** 8kb (before gzipping).
- **Really lightweight:** 7kb (before gzipping).
- **Forward-thinking:** learns from others Router components like the ones of Express and Angular. Written in TypeScript for the future and transpiled to ES5 with UMD format for the present... thus it transparently supports almost every modules' style out there: ES6, CommonJS, AMD. And can be used also as global browser variable (via 'script' tag in your HTML).
- KISS principle: unnecessary complexity avoided.
- Unit tests for every feature are created.
Expand All @@ -43,25 +43,26 @@ import { BrowserRouter } from 'prouter';

// Instantiate the router
const router = new BrowserRouter({
// CSS selector used to obtaing the default target DOM-element
// when sending content (res.send) from the handler
defaultTarget: '.some-css-class'
// (optional) function used to send content (res.send) from the handler
send(content) {
document.body.innerHTML = content;
}
});

// Declare the paths and its respective handlers
router
.use('', (req, res, next) => {
.use('/', (req, res, next) => {
res.send('<h1>Home page.</h1>');
})
.use('about', (req, res, next) => {
.use('/about', (req, res, next) => {
res.send('<h1>About page.</h1>');
});

// start listening events for the routing
router.listen();

// programmatically navigate to any route in your router
router.push('about');
router.push('/about');
```


Expand All @@ -72,25 +73,26 @@ const BrowserRouter = prouter.BrowserRouter;

// Instantiate the router
const router = new BrowserRouter({
// CSS selector used to obtaing the default target DOM-element
// when sending content (res.send) from the handler
defaultTarget: 'body'
// (optional) function used to send content (res.send) from the handler
send(content) {
document.body.innerHTML = content;
}
});

// Declare the paths and its respective handlers
router
.use('', (req, res, next) => {
.use('/', (req, res, next) => {
res.send('<h1>Home page.</h1>');
})
.use('about', (req, res, next) => {
.use('/about', (req, res, next) => {
res.send('<h1>About page.</h1>');
});

// start listening events for the routing
router.listen();

// programmatically navigate to any route in your router
router.push('about');
router.push('/about');
```

### using an interceptor/validator handler
Expand All @@ -100,9 +102,10 @@ import { BrowserRouter } from 'prouter';

// Instantiate the router
const router = new BrowserRouter({
// CSS selector used to obtaing the default target DOM-element
// when sending content (res.send) from the handler
defaultTarget: 'body'
// (optional) function used to send content (res.send) from the handler
send(content) {
document.body.innerHTML = content;
}
});

// Declare the paths and its respective handlers
Expand All @@ -122,18 +125,18 @@ router
// let's continue with the flow in the other handlers below
next();
})
.use('', (req, res, next) => {
.use('/', (req, res, next) => {
res.send('<h1>Home page.</h1>');
})
.use('contact', (req, res, next) => {
.use('/contact', (req, res, next) => {
res.send('<h1>Contact page.</h1>');
});

// start listening events for the routing
router.listen();

// programmatically navigate to any route in your router
router.push('contact');
router.push('/contact');
```


Expand All @@ -147,13 +150,13 @@ import { BrowserRouter, RouterGroup } from 'prouter';
const productRouterGroup = new RouterGroup();

productRouterGroup
.use('', (req, res, next) => {
.use('/', (req, res, next) => {
res.send('<h1>Landing page of Products.</h1>');
})
.use('create', (req, res, next) => {
.use('/create', (req, res, next) => {
res.send('<form>Create a product.</form>');
})
.use(':id', (req, res, next) => {
.use('/:id', (req, res, next) => {
const id = req.params.id;
productService.findOneById(id).then(product => {
res.send(`<h1>${product.name}<h1>`);
Expand All @@ -162,9 +165,10 @@ productRouterGroup

// Instantiate the router
const router = new BrowserRouter({
// CSS selector used to obtaing the default target DOM-element
// when sending content (res.send) from the handler
defaultTarget: '.my-router-outlet'
// (optional) function used to send content (res.send) from the handler
send(content) {
document.body.innerHTML = content;
}
});

// Declare the paths and its respective handlers
Expand All @@ -175,15 +179,15 @@ router
// let's continue with the flow in the other handlers below
next();
})
.use('', (req, res, next) => {
.use('/', (req, res, next) => {
res.send('<h1>Home page.</h1>');
})
// mount the product's group of handlers using this base path
.use('product', productRouterGroup);
.use('/product', productRouterGroup);

// start listening events for the routing
router.listen();

// programmatically navigate to the detail of the product with this ID
router.push('product/123');
router.push('/product/123');
```
94 changes: 43 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "prouter",
"description": "The power of the express's routing-expressions style in the frontend",
"version": "3.1.20",
"version": "4.0.0",
"main": "prouter.min.js",
"homepage": "https://github.com/rogerpadilla/prouter",
"bugs": {
Expand Down Expand Up @@ -51,20 +51,20 @@
"Roger Padilla <rogerpadilla>"
],
"dependencies": {
"path-to-regexp": "^2.0.0"
"path-to-regexp": "^2.1.0"
},
"devDependencies": {
"@types/jest": "21.1.2",
"@types/node": "8.0.33",
"@types/jest": "21.1.5",
"@types/node": "8.0.47",
"awesome-typescript-loader": "3.2.3",
"copy-webpack-plugin": "4.1.1",
"copy-webpack-plugin": "4.2.0",
"jest": "21.2.1",
"phantomjs-prebuilt": "2.1.15",
"rimraf": "2.6.2",
"source-map-loader": "0.2.2",
"tslint": "5.7.0",
"source-map-loader": "0.2.3",
"tslint": "5.8.0",
"typescript": "2.5.3",
"webpack": "3.6.0"
"webpack": "3.8.1"
},
"engines": {
"node": ">= 4.2.1",
Expand Down
Loading

0 comments on commit a74a855

Please sign in to comment.