In SailsJS, how do I check in a view partial that a route is active - sails.js

I have a set of links in a partial that i'd want to change visually depending if the route they go to is active.
Is there a way to check if a route is active from the view?

If I understand correctly, it sounds like you're just looking to see which URL was requested?
The req object, which holds information about the current request, is available as a local variable in all views. So inside your view you can check req.url to determine which URL the user requested. If the view was rendered via a controller using res.view (rather than being rendered directly as a result of a view route), you can also check which controller/action was run by looking at req.options.controller and req.options.action. See the docs on req.options for more info.

Related

How to override the routing and redirect to another view in UI5?

Given the simplified sample app based on Shop Administration Tool. When navigating with the sidebar between the views, I just show different views, e.g. View #1, View #2, etc. These views are standalone views, each of them has its own XML-template and JS-controller.
Now, I want to add a permission check for each view and if a user has no permission, then he should be redirected to the main view instead of the desired view.
I've implemented a pre-navigation check:
router.attachRoutePatternMatched(async (event) => {
const targetView = event.getParameters().view.getProperty("viewName");
const isPermitttedToSeeView = await this.checkUserPermission(targetView);
if (!isPermitttedToSeeView) {
MessageToast.show("Sorry, you don't have permissions.");
router.navTo("mainView");
}
}, this);
This code works but the problem is that in case of no permissions user firstly sees a MessageToast message (OK), then is redirected to the forbidden view (bad), and then immediately redirected to the mainView view (OK).
I've tried to use attachBeforeRouteMatched instead, hoping that in this case the routing is not performed yet and I can redirect a user if needed and user will not see the forbidden view. But not, I still see the forbidden view for a second and then I'm redirected to the mainView.
How can I prevent a redirection to the forbidden view and send the user directly to the mainView view? In other words, how can I alter a routing navigation pipeline?
The possible workaround is to use a «hard» redirect instead of routing:
mLibrary.URLHelper.redirect("%desiredURL%", false);
Where mLibrary is defined in sap.ui.define via "sap/m/library".
This will drop any ongoing routing and will force (in terms of UX, not in terms of security) user to be redirected to the desired URL.
Perhaps, it's not the optimal solution from the performance point of view, since it might lead to unwanted rendering but that's the only way I found to prevent user from being routed to undesired view.

Block in sidebar - where to put the logic?

I would like to create a jumpbox block with some form and put it within the layout sidebar. The form will be have an entity select and exactly the Go button.
Base on the documentation I need to render the form template by using {{ render(controller(...)) }}, but I really don't where tu put the form logic.
It is good to create a method in the controller that use entities from the select? but it looks I need to create two methods, the first one for form rendering (without any route) and the second one for the form submit request (with route for "POST" method)?
Can somebody provide me some tips how to do it right way?
First you will need a controller as you sad, this will render this part of the sidebar, and the action should be the same controller, so just simply create a router for it.
And you were also sad right, create a hidden field and set there the current route. But this is the tricky part, cause when you call this "sub" render, inside the controller the route will be always, what is the route for the controller, so what you need to do, when you render the controller you need to pass in a variable what is the current route, what you can do easily by passing the {{ app.request.attributes.get('_route') }} variable value, what is the NAME of the route, and then in your controller, at the end you return a new RedirectResponse($this->generateUrl($url)).
And both, the render of the form and the "process" can be in the same controller, or if you prefer it, you can take it apart, but I would use only one, and you can test from the request, what is the current method, if POST then you will search for the variable and set the session/cookie/ what do you have.
EDIT:
Even though you didn't like, you have to say it's a good answer, but here is an other one.
The action should be always the current route, and basically you need to set up a request event listener. Check there if it's a post method and if yes, then look for your specific key, and there you go. Both is equally good and I used both of them.

ASP.Net MVC 3 - Single controller, single action and multiple views

I would like to know what is the best method to have an action return different views. Let's say you have a form for submitting data, but you want to choose the view depending on what data is submitted. I would prefer not using a redirection, since there is stuff I want to be displayed in the data that is posted.
An example of this would be to have an Edit form that displays a Details view when clicking on Save, but without using a redirection.
I know this could be done with a single view containing a conditional if statement to display this or that, but there are cases where I would prefer my views to stay simple without too much code in them. If the controller could just choose the view to display once the data is posted, this would be great.
There is an overload to the View() method that allows you to specify the name of the View you want to return.
return View("DetailsView", model);
You should be using the Post/Redirect/Get pattern. You can still "display stuff." You can pass an ID on the URI and look them up in the new GET or use TempData.
Attempting to circumvent Post/Redirect/Get is not a good solution. Among other things, it breaks the back button.

Returning object model to a view, that is handled by different controller

how can I pass an object model to a view, that is partial view on a master page?
regards
You might consider creating an another object that more closely represents the view you are trying to render.
Let's say i have an MyDomain.Order object, so I make a view page that looks something like ViewPage<MyDomain.Order>. Now, let's say that I have a menu that is driven off of a logged in user, as example. It wouldn't make sense to have menu as a property of MyDomain.Order. I would create another object, specifically for the view, call it something like OrderPageModel and have MyDomain.Order and List<MenuItem> as properties of this new object, my view being set up as ViewPage<OrderPageModel>.
The other thing to consider might be something like Html.RenderAction(). Same scenario, I have a view, and as you mention in your question, it has a master page, and as in my example, lets say it hosts a menu common to your site. You could create a partial view (UserMenu.ascx) and a controller (SiteController.cs) with an action (UserMenu) that calculates the items for the menu. Inside your master page, you can then call <% Html.RenderAction("UserMenu","SiteController") %>.
I would use the first example if it could be something made for a particular view: just make it a part of the model. I would use the second example if it was something more generic to the site, like a menu.
You could specify the location of the view:
return PartialView("~/Views/SomeOtherController/SomePartial.ascx", someModel);
Best bet here is RenderAction over RenderPartial. Your child controller can easily figure out if the user is logged in and render the right partial rather than making your master page worry about these details.

Discover which (if any) route will match a given URL from within Controller in ASP.NET MVC2 app

In my master page I have a placeholder where a Html.RenderPartial() will render a collection of (bread)crumbs which are effectively a list of links I build up using action, controller names and RouteValueDictionary's.
I have an action that is called from multiple places to view a short-list and so when building the list of breadcrumbs for this actions view to display. Ideally I'd like to use Request.UrlReferer as the penultimate crumb.
Before unconditionally using this URL I want to check that it will actually match at least 1 route so I can be sure if the user clicks it they will get a view from my app and if they don't I will simply use the home page instead.
Any suggestions how I would go about this?
Take a look at this post http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx