getUserByToken at auth.service call double - metronic

I'm try using metronic today, and see that method getUserByToken at auth.service always call double, it is normal or some bug? because when we start using ajax post to populate user detail, this will call same ajax 2x.
I'm using metronic 7 with angular.

I'm found root cause why this always double ajax
auth service constructor called
auth.service.ts:72 getUserByToken called
app.module.ts:23 APP_INITIALIZER appInitializer(authService: AuthService) called
auth.service.ts:72 getUserByToken called
in app module define APP_INITIALIZER to check token, and in constructor of class already create subscribe. But I don't know while I remove subscribe at constructor effect.

Related

TYPO3 pass controller context to ajax request

I am working on a TYPO3-backend-module. The main "overview"-Action allows to upload a file via ajax upload, which gets processed in one of my controllers.
The corresponding controller-action instantiates a StandaloneView, renders it and writes the rendered content to the response body.
My problem is, that I have no controllerContext set in the generated content, therefore a form, which should trigger a different extbase action, in that rendered content does not work, because the modulename is not set.
Can anybody tell me how I could possibly get/pass the controllerContext, to make it available in the controller action, which is called via ajax ?
Best regards and thanks in advance, Oliver !
The main problem here is that BE modules don't exactly use a standard View implementation - in the default implementation, they wrap around a StandaloneView which by definition has no controller context.
That being said, if you can change the way the View wraps around the StandaloneView, the controller context can be copied by doing:
$view->getRenderingContext()->setControllerContext($parentView->getRenderingContext()->getControllerContext());
Alternatively you may want to copy the entire rendering context rather than just the controller context.
Advise applies to TYPO3v8 only. On 7.6 you will need to copy the Request instead.
For usages outside of MVC context you will need to manually pass the original controller context values such as controller name and module name. That means the values must be part of the link you create and must be read by the receiving code (in whichever way that code's context requires) and then manually assigned to a Request you create and pass to the sub-view. Note that this is only necessary if those MVC context values actually change - if you always call the same action next, you can get the right result by simply hardcoding the desired extension name, module name, controller name and action in the form/link in the template you render from that AJAX controller.

Partial markup via Tapestry block

I'm using Tapestry 5.3.
In a Tapestry component, I'm trying to implement an event handler that creates responses to AJAX requests.
I know that I can call MarkupWriterFactory#newPartialMarkupWriter to get an instance of MarkupWriter and then generate the response using this instance. The built-in mixin Autocomplete generates responses to AJAX requests in this way. Here is a trivial example:
#Inject
private MarkupWriterFactory markupWriterFactory;
#OnEvent("myevent")
Object generatePartialMarkup() {
final ContentType contentType = new ContentType("text/html");
final MarkupWriter markupWriter = markupWriterFactory.newPartialMarkupWriter(contentType);
markupWriter.element("hr");
markupWriter.end();
return new TextStreamResponse(contentType.toString(), markupWriter.toString());
}
However, this is a clumsy solution for a partial HTML that is much more complex than just a hr element. I wonder if there is a chance to create such response using a Tapestry block rather than generating the response "manually" via MarkupWriter. If I just return an instance of block from the event handler, an exception is thrown when the event is triggered.
Thanks.
Looks like you're making life difficult for yourself. From what I can see you can solve this with an eventlink, a zone and an event handler which returns a Block. See here for more info.
Not entirely sure what you're doing but if you need to render a block as a string, see the capture component here

angular 2 route to 404 page when route param is invalid

Say I have an route with a param like this (in Angular 2): /user1/products/:id, which could have child routes like /user1/products/3/reviews.
When I navigate to /user1/products/-1, I check with my server and if the product doesn't exist, I want to render a 404 page without affecting the browser history.
Using router.navigate(['/404']) or router.navigateByUrl('/404') doesn't seem to work because it adds both /user1/products/-1 and/404 to the browser history.
Meaning when I press the Back button in the browser, I go back to /user1/products/-1 which is immediately redirected to /404 and I'm essentially stuck at /404.
In ExpressJS, we would do something like next() to pass the request to our 404 handler. Is there a client-side equivalent in Angular 2?
Update
In the new Router V3 you can use guards as explained in https://angular.io/guide/router#canactivate-requiring-authentication
Original
I think you should use #CanActivate() to do the check. If you forward in #CanActivate() the invalid URL shouldn't be added to the history (not tried)
See also https://github.com/angular/angular/issues/4112 for how to use DI in #CanActivate()
Ok, this is already implemented, just not well-documented.
According to the docs:
router.navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>
Has a parameter _skipLocationChange that will not modify the history.
These will do the trick:
router.navigateByUrl('/404', true);
router.navigateByInstruction(router.generate(['/404']), true);
As of Angular 2 final this is the solution:
this._router.navigateByUrl('/404', { skipLocationChange: true })
Its really interesting question, perhaps you should report it as feature request. I would be nice to have access to router instruction inside loader callback of RouteDefinition.
You could try to emulate validation adding default route /** and using regex parameter of RouteDefinition to match only positive numbers.

How do i page using forms with the Codeigniter framework?

I'm using the Codeigniter framework and am in the process of creating a registration form. The registration process is completed in multiple steps- for which I've created different views.
What I have a problem with is making the controller read that I've continued to a new step. I tried solving this by posting the form to index.php/controller/2 but as I reach the page I get a 404 error stating
The page doesn't exist.
I've loaded the URI helper so I don't quite understand where the problem lies.
All help is very much appreciated
By submitting the form to index.php/controller/2 you're effectively saying
Load /application/controllers/Controller.php
Instantiate class Controller
Run Controller::2()
I suspect you don't have a method named 2, and you want to pass two as an argument to a method which handles step 1. Which might be /controller/register or similar.
You need to submit your form to index.php/controller/method/2 and inside method check which step you're on using $this->uri->segment(2)
Ideally, create a different method for each step as it'll better separate the logic. For example
class Registration {
function step_1() {}
function step_2() {}
}
Which will allow you to call index.php/registration/step_1/ and index.php/registration/step_2/ for example.
You may also wish to use the Session class to set variables indicating which stages are complete to prevent people skipping to other stages by typing in the URL.

JSF 2 Scriptmanager style functionality

I need to be able to add some javascript to all ajax postback responses (PartialViewContext.isAjaxRequest == true) but I am not succeeding with any implementation I try.
I have tried implementing a
PhaseListener
and adding my script using PartialResponseWriter.insert* to add eval blocks, as well as trying to add the script by creating a script element. (Results in CDATA cannot nest, or just invalid XML)
I have tried decorating PartialViewContextFactory to override the
PartialViewContext.processPartial
and add the script after the wrapped instance has processed it...
How should I go about adding sripts to an Ajax response? Something similar to what .NET has with Scriptmanager.registerClientScriptBlock preferably.
Thank you
Use the javascript libraries that come bundled with your JSF2 implementation (as required by the spec). Here are the API docs:
http://java.sun.com/javaee/javaserverfaces/2.0/docs/js-api/index.html
The jsf.ajax.addOnEvent solves your problem of "adding some javascript to all ajax postback responses":
This function must accept a reference to an existing JavaScript function. The JavaScript function reference must be added to a list of callbacks, making it possible to register more than one callback by invoking jsf.ajax.addOnEvent more than once.

Resources