broute - brutal router
import {route, path, navigate} from 'broute'
route
route(f)
Declares the route function f
which will be invoked each time the
url changes. There can only be one such function. The url is
"consumed" using nested scoped path
functions.
:: (() ->) -> undefined
f | The one and only route function. |
ret | Always undefined |
route usage
The following usage shows how nested path
declarations creates
"scoped" functions that consumes part of the current url.
route(() => {
path('/some', (left, query) => {
path('/path/deep', (left, query) => {
})
})
path('/another', () => {
})
})
route example
isItem = (part, query) => part?.length > 1
route(() => {
path('/news/', (slugId) => {
action('selectarticle', slugid)
}, () => {
action('refreshnewslist')
})
path('/aboutus', () => {
action('showaboutus')
})
})
path
path(p,f,fe)
As part of route
declares a function f
that is invoked
if we "consume" url part p
of the current (scoped) url.
p | The string url part to match/consume. |
f | Function to invoke when url part matches. |
fe | Optional else function if url part doesn't match. |
path example
See route usage and route example.
navigate
navigate(l)
navigate(l, false)
Navigates to the location l
using pushState and checks to
see if the url changed in which case the route function is
executed.
The function takes an optional second boolean argument that can be
used to supress the execution of the route function.
This function is lazy when used inside route, only the last
location will be used when the route function finishes.
:: string -> undefined
:: string, boolean -> undefined
l | The (string) location to navigate to. Can be relative. |
t | Optional boolean set to false to supress route function triggering. |
ret | always undefined |
navigate example
navigate('other')
navigate('/news')
navigate('/didnt', false)
License
The MIT License (MIT)
Copyright © 2016 Martin Algesten
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.