Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent navigation? #2

Open
kokujin opened this issue Feb 18, 2015 · 26 comments
Open

Prevent navigation? #2

kokujin opened this issue Feb 18, 2015 · 26 comments

Comments

@kokujin
Copy link

kokujin commented Feb 18, 2015

I would like lightrouter to detect the page that a user is on and allow me to run certain code blocks, is it possible to make lightrouter prevent navigation to a matched route?

Thanks!

@garygreen
Copy link
Owner

What's your code so far?

@kokujin
Copy link
Author

kokujin commented Feb 18, 2015

Have'nt written anything yet, I was going through the docs

@garygreen
Copy link
Owner

By the sounds of it your trying lightrouter in a single page app?

@kokujin
Copy link
Author

kokujin commented Feb 18, 2015

its actually a hybrid. Some parts mimic a single page app, some parts traditional. I thought I could run javascript code conditionally depending on the URL using lightrouters matching.

@garygreen
Copy link
Owner

You can easily do it with traditional path url, but if using in a single page app you would have to create a bit of additional code to listen for hashbang changes, run router, add to history if matching etc. Lots of other javascript routers are more 'heavy weight' and do all this for you, lightrouter is more of a foundation for matching paths and calling code conditionally.

I have been meaning to create a plugin for light router to make it easier to integrate it will SPA out of the box, something definitely on the todo list.

@kokujin
Copy link
Author

kokujin commented Feb 18, 2015

Yeah, actually, my focus is only running code based on a path on the traditional urls. No pushstate or hashbang sniffing. How can I achieve this?

@garygreen
Copy link
Owner

In which case it's very easy, the docs show exactly how to use the lightrouter. What part of the docs are you having trouble understanding? (Maybe needs improvement)

@kokujin
Copy link
Author

kokujin commented Feb 18, 2015

Here is an example of what I want to achieve using another library called byroads(https://github.com/W3Max/byroads.js), I can declare a route, and then test if it matches. Navigation is not triggered.

 var byroads = window.returnExports;
 var pageRE = new RegExp('^/(.*)-xk([0-9]*).jsp(.*)');
 var route = byroads.addRoute(pageRE);
 console.log('match ',route.match('foo')); // false
 console.log('match ', route.match(location.pathname)); // true, if pathname matches regex

I could not find an example that I could use for my case.

@garygreen
Copy link
Owner

It's technically possible to test if a specific route matches like that, but this router does it generally like:

var router = new LightRouter({
    type: 'path',
    path: 'testing-xk500.jsptesting'
});
router.add(/^(.*)-xk([0-9]*).jsp(.*)/, function(params) {
    console.log('matched', params);
});
router.run();

You don't need to supply path to lightrouter it will automatically detect it from the url's path OR hash if you specify type as hash (see examples from docs). I've only added it for testing in the example above..

@garygreen
Copy link
Owner

If you wanted to test the routes manually you could do something like:

var router = new LightRouter();
router.add(/^(.*)-xk([0-9]*).jsp(.*)/, function() { });

for (var i = 0; i < router.routes.length; i++)
{
    console.log(router.routes[i].test('foo')); // false
}

Or if your only testing one route:

var router = new LightRouter({
    type: 'path',
    routes: {
        '^(.*)-xk([0-9]*).jsp(.*)': function() { }
    }
});

console.log(router.routes[0].test('foo'));

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

Nice! Thanks

@kokujin kokujin closed this as completed Feb 19, 2015
@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

I spoke to soon, the example below causes an error

var router = new LightRouter({
    type: 'path'
});
router.add(/^(.*)-xk([0-9]*).jsp(.*)/, function(params) {
    console.log('matched', params);
});
router.run();
 Uncaught TypeError: undefined is not a function

The recursive call to "run" fails

        run: function() {
            var url = this.getUrl(), route;

            for (var i in this.routes)
            {
                console.log('///////');
                // Get the route
                route = this.routes[i];

                // Test and run the route if it matches
                route.test(url) && route.run(); <--- fails
                console.log('|||||||');
            }
            return this;
        }

@kokujin kokujin reopened this Feb 19, 2015
@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

This works

run: function() {
            var url = this.getUrl(), route;

            for (var i in this.routes)
            {
                // Get the route
                route = this.routes[i];
                if( this.routes.hasOwnProperty(i) ){
                    // Test and run the route if it matches
                    route.test(url) && route.run();
                }                   
            }
            return this;
        }

@garygreen
Copy link
Owner

Are you running this in a browser/node?

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

Browser, chrome and FF

@garygreen
Copy link
Owner

I suspect your using some other js that has prototyped arrays like Array.prototype.someproperty = 'blah'

Can you try using only lightrouter in your app and see if it works?

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

I am actually using a shim lib(es5-shim.js) since we have to support older IE versions. But removing it does not help, same problem.

@garygreen
Copy link
Owner

If you clone this repository and opentests/browser/index.html in your browser, do all the tests pass? I still think it's some dodgy js or even maybe chrome extension that is prototyping an extra key/property onto arrays...

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

do I need anything else
I called called "http-server" in the tests folder, the server starts up on port 8080 but the tests are not run

passes: 0failures: 0duration: 0s

@garygreen
Copy link
Owner

Just open tests/browser/index.html in your Chrome browser locally. It should run the tests

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

ok, all tests pass, sorry about the delay

passes: 23failures: 0duration: 0.05s

@garygreen
Copy link
Owner

Just pushed a new version, try 0.2.2 and let me know if it fixes your issue

@kokujin
Copy link
Author

kokujin commented Feb 19, 2015

Ok,will do. Have you pushed it? The repo ist still showing an old commit

@garygreen
Copy link
Owner

Yeah I have pushed it, it's on tag 0.2.2 so should be able to pull it down. Master is showing as 2 commits behind, not sure why......

@garygreen
Copy link
Owner

@kokujin just released new version, does this fix any issue you were having?

@garygreen
Copy link
Owner

@kokujin I've just released another version, you can match routes manually now with .match(route, callback) which might be more suited to your particular app. Let me know how it goes :-)

@garygreen garygreen reopened this May 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants