Meteor: Iron.Router Catch All (Will match the rest of the URI) - meteor

Is it possible to define a Iron.Router route with a parameter that will match the rest of the URI?
For example
Router.route('results', {
path: '/test/:domain'
});
This will match on entries like
/test/hello
/test/hello.com
What I really need, is to also match on entries such as
/test/hello.com/about
/test/hello.com/about?param=3
Thoughts?

Figured out how!
In this case the path will now be
Router.route('results', {
path: '/test/(.*)'
});
To access the trailing info, access the parameter at index 0
this.params[0]

Related

How to redirect from `/` to `/foo/<id>` using FlowRouter and MeteorJS?

In my scenario, I want everyone that visits our root URL to be auto-redirected to a url containing a document for collaboration and instant gratification.
Here, the router.coffee code is:
FlowRouter.route '/',
action: ->
console.log "I'm home!"
FlowRouter.go 'myProject'
name: 'myHome'
FlowRouter.route '/my/:projectId',
subscriptions: (params) ->
#register 'currentProject', Meteor.subscribe 'project', params.projectId
action: ->
BlazeLayout.render 'myBody'
name: 'myProject'
I want the root URL to redirect to /my/:projectId but I'm unsure of how to retrieve the auto-generated projectId and redirect using with either FlowRouter.go or FlowRouter.redirect.
Is this possible?
If yes, how?
Thanks for your help!
Since the data may not be available when the route action execute,
the best is to re-route at the template level.
It might be a good idea to use the Template.[name].onCreated() function
and put inside it something like the following code:
pID = ... // Get the user project ID from wherever you saved it
var params = {projectId: pID};
// Set the project URL including the :projectId parameter and re-route the user
FlowRouter.go("myProject", params);

Iron Router - Get Route URL as Variable/String

In Iron Router, I can get the URL of the route and redirect by doing...
Router.go('ROUTE_NAME', { param: parm })
This returns the url (i.e. /whatever/whatever) and then redirects to that url.
How can I get JUST the URL and NOT redirect?
You can access the route object directly and ask for the path:
Router.routes['ROUTE_NAME'].options.path
or
Router.routes['ROUTE_NAME'].path()
or, if you want the absolute URL:
Router.routes['ROUTE_NAME'].url()
If you want the curent url:
Router.current().url
this is how to process a route and get it as string :
> Router.url("your.url.with.:param1.:optional?", {
param1: "azertyui",
optional: "qsdfghjk"
});
"http://localhost:3000/your/url/with/azertyui/qsdfghjk"

Is routes case-sensitive in Web API OData Service using ODataController?

i followed this to learn how ODataController works, everything is OK but when i changed the request uri
from
"localhost:49292/odata/Employees" //result: 200
to
"localhost:49292/odata/employees" //result: 404
to say one word: "odata" or "Odata" and "Employee" are all ok, but lowercase "employee" return 404. any explanation about this. Moreover, the routes in asp.net mvc is not case-sensitive afaik.
how about including a Route attribute and direct it to lower case. for Upper case web api will take care about it
[Route("odata/employees")]
add this on the top of the controller
if odata is common for every action then you can include [RoutePrefix] attribute
You can manually do it using the ODataModelBuilder instead of the ODataConventionModelBuilder
e.g
var builder = new ODataModelBuilder();
builder.EntitySet<Order>("Employees");
builder.EntitySet<Order>("employees");
this will work but your metadata will show 2 entity sets:
{
#odata.context: "http://localhost:62881/$metadata",
value: [
{
name: "Employees",
kind: "EntitySet",
url: "Employees"
},
{
name: "employees",
kind: "EntitySet",
url: "employees"
}
]
}
lowercase "employee" return 404.
I hope you probably didn't have the typo like that.
AFAIK, there is a case limitation on filter and properties. (You can vote there https://aspnetwebstack.codeplex.com/workitem/366 ) but not sure about the controller name..
You can create the REST server using web api without having oData as well..

trying to use params on main route (/) of my meteor app

I'm trying to use params on my main route. But in fact the params are not set and they are used in the path :
Router.map(function funcClientRouterMap(){
this.route('home', {
path: '/:_redirect?',
action: function funcClientRouterMapAction(){
console.log(this.path, this.params);
}
})
});
now if i try manual redirection here is what i get :
Router.go('home'); // it redirects on / => ok
Router.go('home', {_redirect: test}); // this.path = /test, and this.params is empty
How can i use _redirect like a params and not a route ?
Thanks
Router.go accepts a path as its first argument (per the docs). So if you were trying to programmatically cause the same result as the user going to /redirectMeSomewhere, you just use:
Router.go('/redirectMeSomewhere');
And this.params._redirect should be 'redirectMeSomewhere'.
Note that like #apendua implies, if you have other routes it could cause chaos to have a route defined as /:anything, because the other routes may never get triggered. If the above doesn't do the trick, try commenting out all your other routes to see if that changes anything.

How to handle / ignore a bad route with durandal?

I'm trying to conditionally block a route from being accessed. I think this can be done with guardRoute: http://durandaljs.com/documentation/Router/
function guardRoute(routeInfo, params, instance) : object - Before any route is activated, the guardRoute function is called. You can plug into this function to add custom logic to allow, deny or redirect based on the requested route. To allow, return true. To deny, return false. To redirect, return a string with the hash or url. You may also return a promise for any of these values.
I'm not sure how to specify which route should be accessed though, or how to re-route to another view if I need to deny access. Can someone post an example of its use in this manner?
You should use guardRoute before activating the router e.g. in shell.js.
The example is taken from a Durandal 2.0 alpha site. AFAIK guardRoute hasn't changed from 1.2, but setting a breakpoint will allow you to figure out what arguments are passed in for 1.2.
As a general rule, return true to allow navigation, false to prevent it and a hash or url value to redirect.
define(['plugins/router'], function (router) {
// Redirecting from / to first route in route.map
router.guardRoute = function(routeInfo, params, instance){
if (params.fragment === ''){
return routeInfo.router.routes[0].hash;
}
return true;
};
...
return {
...
router: router,
activate: function () {
router.map([
---
]);
return router.activate();
}
};
});
I am a newbie to durandaljs 2.0 having only used it for a couple of weeks so this was one of my first stumbling blocks. see my code below
router.guardRoute = function (routeInfo, params, instance) {
var insecureRoutes = ['login', 'terms','signup','password'];
if ($.inArray(params.fragment, insecureRoutes) || mymodule.isAuthenticated()) {
return true;
} else {
return 'login/' + params.fragment;
}
};
I also define a route for the login page like this
{ route: 'login/(:returnroute)', moduleId: 'viewmodels/login', nav: false },
This allows you to optionally specify a return rout so that after login you can redirect the user to where ever it is they were initially trying to go to. I hope it helps someone

Resources