Last active
August 29, 2015 14:23
-
-
Save barneycarroll/651a6cbbdc6075d8f8f8 to your computer and use it in GitHub Desktop.
A custom attribute for routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function route( node, route, attrs ){ | |
if( route instanceof String ) | |
route = [ route, {} ] | |
var uri = route[ 0 ] | |
var params = route[ 1 ] | |
var config = attrs.config | |
for ( var key in params ) | |
if ( params.hasOwnProperty( key ){ | |
var placeholder = ':' + key | |
var delimiter = uri.indexOf( '?' ) > -1 ) | |
? '&' | |
: '?' | |
if( uri.indexOf( placeholder ) > -1 ){ | |
uri = uri.replace( | |
placeholder, | |
params[ key ] | |
) | |
} | |
else { | |
uri += delimiter + params[ key ] | |
} | |
} | |
uri = uri.replace( /:([^\/:?&#]+)/g, function( match, param ){ | |
return m.route.param( param ) | |
} ) | |
node.href = uri | |
node.config = config | |
? function(){ | |
config.apply( this, arguments ) | |
m.route.apply( this, arguments ) | |
} | |
: m.route | |
} |
Author
barneycarroll
commented
Jun 13, 2015
- Todo: fill in unspecified placeholders from the current route
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment