Giter Club home page Giter Club logo

routie's Introduction

#Routie

Routie is a javascript hash routing library. It is designed for scenarios when push state is not an option (IE8 support, static/Github pages, Phonegap, simple sites, etc). It is very tiny (800 bytes gzipped), and should be able to handle all your routing needs.

##Download

##Basic Usage

There are three ways to call routie:

Here is the most basic way:

routie('users', function() {
	//this gets called when hash == #users
});

If you want to define multiple routes you can pass in an object like this:

routie({
	'users': function() {

	},
	'about': function() {
	}
});

If you want to trigger a route manually, you can call routie like this:

routie('users/bob');  //window.location.hash will be #users/bob

##Regex Routes

Routie also supports regex style routes, so you can do advanced routing like this:

routie('users/:name', function(name) {
    console.log(name);
});
routie('users/bob'); // logs `'bob'`

###Optional Params:

routie('users/:name?', function(name) {
    console.log(name);
});
routie('users/'); // logs `undefined`
routie('users/bob'); // logs `'bob'`

###Wildcard:

routie('users/*', function() {
});
routie('users/12312312');

###Catch All:

routie('*', function() {
});
routie('anything');

##Named Routes

Named routes make it easy to build urls for use in your templates. Instead of re-creating the url, you can just name your url when you define it and then perform a lookup. The name of the route is optional. The syntax is "[name] [route]".

routie('user users/:name', function() {});

then in your template code, you can do:

routie.lookup('user', { name: 'bob'}) // == users/bob

##Dependencies

None

##Supports

Any modern browser and IE8+

##Tests

Run make install, then make test, then go to http://localhost:8000/test

routie's People

Contributors

ctrlc-root avatar danschumann avatar davidsouther avatar jgallen23 avatar nairvijays avatar thedanheller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

routie's Issues

the routine to get the regex of a wildcard parameter will cut the end off the value

e.g.
routie("thing/*:wildParam", function(wildParam){});

for #thing/theValue you only get theValu (missing the e)

so was thinking, wildcards can only happen once and as the last param why not get rid of the : (colon) in the declaration and handle them separately in pathToRegexp?

e.g.
routie("thing/*wildParam", function(wildParam){});

in pathToRegexp instead of

.replace(/\*/g, '(.*)')

use this

.replace(/\*(\w+)/g, function (_, key) {
          keys.push({ name: key });

          return '(.*?)';
      })

what do you think?
works for me
can you see any problems?

ps. thanks for the lib

Route doesn't fire when you're already on it.

I know this sounds sort of silly, but when you're already on a route, say:

/#/add-photo

and you click the button which activates that route again, the route function does not trigger. While there could be a usecase for this, I think it's consistent with how browsers work. Even if you're already on a page, clicking the button which loads that page still functions properly.

Thoughts?

lazyload routes

Is there any way of adding additional routes without causing a routie.reload?

e.g.
current hash is #text

routie('text', textFn);
if (imagesExist()) {
  routie('images', imagesFn);
}

results in textFn being triggered twice

Fallback route

When adding a catch-all route using '*', it seems it gets executed even if other routes matched.

How would you go about adding a fallback route, for ex:

routie({
    '/foos':   MyApp.Foos(),
    '/bars':   MyApp.Bars(),
    '*':       MyApp.SomethingLikeA404()
});

With this configuration, /foos triggers 2 controllers one after the other.
Thanks,

Inconsistency in documentation

On http://projects.jga.me/routie/ it says this about optional parameters:

routie('users/?:name', function(name) {
    //name == undefined
    //then
    //name == bob
});
routie('users/');
routie('users/bob');

But here on GitHub the documentation says this:

routie('users/:name?', function(name) {
    //name == undefined
    //then
    //name == bob
});
routie('users/');
routie('users/bob');

?:name or :name?
Which is the correct one?

Feature request

Hello!
Can you, please, provide some kind of components extraction from routie?
For example: somewhere in the code (not in the routie callback) I can access routing params, ex.:
var id = rouie('getPathComponents', 'id');
or
routie('getPathComponents', function(components) {
var controller = components[0];
var action = components[1]
var param1 = components[2];
});
Thanks for the lib :)

Error Handling

I don't see a way to provide handlers for '404 errors' (when a route has not been defined).

Routie seem to ignore multiple clicks on the same route

I have some HTML that looks like

  • New Assay
  • I have routie set up as

        routie({           
            'new/:entity':function (entity) {
                if (entity == 'project') handleNewProject();
                else if (entity == 'assay') handleNewAssay();
            }
        });
    

    The first time I click on the link, routie catches the new URL and appropriately executes the function above. The URL in the url bar now reads like: https://localhost/foo/#new/assay

    But if I click on the link a second time, nothing seems to happen. I've stepped through the JS code and routie never seems to be invoked after the first time I click on the link.

    Am I doing something wrong? This is using routie 0.3.2 with Chrome v29 on OS X

    How get Routing Paramters

    I need get parameter from current rout.
    like this : /#chartofaccounts/1992e710-2f9f-4a22-9d29-0e92e30467be
    my rout is : 'chartofaccounts/:accountid?': chartOfAccounts,

    function chartOfAccounts(accountid) {

    }

    how I can read accountid out chartOfAccounts function

    using browser back button on :number wildcard

    I use the :number wildcard to define my routie:

    routie('question/:number', function (number) {
      //some code
    });
    

    now if i go for example from my URL #question/7 to #question/9 and press the back button in my browser i go to #question/8, which should of course end up at #question/7 (because i skipped #8). Any way to fix this behavior?

    Allow whitespace when declaring named routes

    Named routes are great, but unfortunately don't support whitespace in their declaration:

        'ITEM_HOME /items/:slug':                    controller1,
        'ITEM_DETAIL /items/:slug/details':          controller2,
        'ITEM_COUNTRY /items/:slug/country/:code':   controller3
    

    would read a lot easier as:

        'ITEM_HOME      /items/:slug':                 controller1,
        'ITEM_DETAIL    /items/:slug/details':         controller2,
        'ITEM_COUNTRY   /items/:slug/country/:code':   controller3
    

    Multiple routes trigger first one to fire multiple times

    I have a following code:

    routie('search/:text', function(text) { ... });
    routie('view-group/:id', function(id) { ... });
    routie('view-test1/:id', function(id) { ... });
    routie('view-test2/:id', function(id) { ... });

    When reloading a page, first handler ('search/:text') when route is matched is fired 4 times, for each route declared. If I remove view-test1 and view-test2, it is fired 2 times.

    Is this a serious bug?

    default route

    Hi. Thank you for a great plugin! It simply just work.
    I do have a question: Is it possible to create an event for default route when not mathing any other routes? This means only call if not matching any of the configured routes?

    Callback/Controller function called multiple times

    routie('', function(){
        console.log('called')
    })
    routie('foobar', function(){
        return false
    })

    console:
    called
    called

    However, this code:

    routie({
      '': function(){
        console.log('called')
      },
      'foobar': function(){
        return false
      }
    })

    console:
    called

    So the second sniped works fine, first one does not...

    Chrome 43.0.2357.81 m
    Win7 SP1 64

    add to npm?

    We're using routie via bower right now, but we've been shifting things over to npm. Would you mind pushing a package there as well? There's a pre-existing package there, so it might have to go under a different name...

    Double routing

    route = function(){
        '': function() {
            routie('splash')
        },
        'splash': function() {
            console.log('splashing')
        }
    }

    When navigating without # it activates the 'splash' function twice in webkit browsers

    tested on:

    • linux chrome (activates twice)
    • linux firefox (activates once)
    • osX safari firefox (activates once)

    before route?

    Hi,

    have some method as before route where I can implement a logic before routing?

    thank you

    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.