Skip to content

Commit

Permalink
feat(mapper): change a way to pass query option to mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Jul 15, 2016
1 parent 8ea1fc0 commit 81baf4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ controller.addModules({
// baseUrl: '/', // base part, that ignored on route match. detected automatically if `onlyHash` option set to true
// preventAutostart: true, // prevents automatic triggering after `modulesLoaded` event
// allowEscape: true, // do not prevent navigation for triggered urls if no routes was matched and catch all route wasn't provided
mapper: { query: true } // options passed to url-mapper
query: true // option to enable query support in url-mapper
})
)
```
Expand Down Expand Up @@ -144,15 +144,15 @@ Given that you still be able to disable router at any time.
Path-to-regexp is pretty powerfull, but sometimes you want your url would hold more information to pass your app.
Usually it is done through queries. Using the same considerations as in previous point we decided that types should be preserved.
We can enable query support with [`urlon`](https://github.com/vjeux/URLON) super powers of stringify/pasrse any JSON compatible object (any payload can be passed to signal, as fact).
Just pass `mapper: { query: true }` to router options and any payload not defined in path part would be stringified to query part.
Just pass `query: true` to router options and any payload not defined in path part would be stringified to query part.
It is not easy to construct `urlon` queries by hands, but you never had to. Just keep reading.
### Create links
Most times you need to have a links in app. It enables sharing url, opening url in new tab, etc.
You can use [`getSignalUrl`](http://cerebral.github.io/cerebral-module-router/index.html#_index_d_.routerservice.getsignalurl) method exposed by router to avoid hardcoding urls within app.
Please follow your view package documentation to see if it already have component to make a links.
Fell free to create an issue on that package otherwise.
Feel free to create an issue on that package otherwise.
Link component will fallback to `onClick` event handler if router is absent.
So you can disable router at any time even if you are using links.
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ function Router (routesConfig, options) {
options.baseUrl = addressbar.pathname
}
options.baseUrl = (options.baseUrl || '') + (options.onlyHash ? '#' : '')
var urlMapper = Mapper(options.mapper)

if (options.mapper) {
options.query = options.mapper.query
console.warn('Cerebral router - passing options object to mapper is deprecated. Use router `query` option directly.')
}

var urlMapper = Mapper({ query: options.query })

return function init (module, controller) {
var signals = getRoutableSignals(routesConfig, controller.getSignals())
Expand Down
13 changes: 11 additions & 2 deletions tests/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ module.exports = {
controller.addModules({
router: Router({
'/foo/:bar': 'foo'
}, { mapper: { query: true } }),
}, { query: true }),
devtools: function () {
controller.emit('predefinedSignal', { signal: { name: 'foo' }, payload: { bar: 'bar', baz: 'baz' } })
}
Expand Down Expand Up @@ -443,6 +443,15 @@ module.exports = {
test.done()
},

'should warn mapper options deprecation': function (test) {
test.doesNotThrow(function () {
Router({}, { mapper: { query: true } })
})

test.equal(this.warnMessage.length >= 0, true)
test.done()
},

'should throw on missing nested signal': function (test) {
var controller = this.controller
this.controller.addSignals({
Expand Down Expand Up @@ -510,7 +519,7 @@ module.exports = {
route: '/',
options: {
baseUrl: '/test',
mapper: { query: true },
query: true,
onlyHash: true
}
})
Expand Down

0 comments on commit 81baf4a

Please sign in to comment.