grails redirect to another view - grails3

i am trying to create status page which check database for the job status every 5 minutes and then updates the user about the status of the job..
So once the job is finished, I want to move from the current page to result page..
To update the status i m using JavaScript :
function() {
setInterval(function() {
var uuid =" \'${uuid}\'";
var jobid =" \'${jobid}\'";
console.log();
$('#checkstatus').load('/jobque/connectdatabase',{jobid:jobid,uuid:uuid});
}, 5000);
});
Checking status works correctly. I tried using redirect or forward but both these terms loads the result page into current page. I want to go to next page not load into current page ..
What are the other option available ?

Your javascript uses jquery to load the results of a call to a controller action into the named element.
Why not check the result of your call to the controller action and if it indicates success, then use javascript to redirect to your desired page.
controller action:
def checkStatus() {
if (status_check_ok) {
render 'url_of_page_for_redirect'
return
}
render status:409 // failure
}
javascript on web page:
$.ajax({
timeout:5000,
type: 'POST',
url: controller_action_url_for_checkStatus,
success: function(data){
window.location.href=data
},
error: function(data){
// do something else
}
});

Related

Nuxt.js search by query param without reload

What are the steps needed to make a redirect to search results without page reload in Nuxt.js? So far, it's been sorted this way:
search() {
location.href = '/search?phrase=' + this.phrase;
},
and I need to make it happen without page refresh. In the page's dir is a search directory with index.vue inside like
/search/index.vue
so far I found some solutions to use $router.on(event) so I have something like this
created() {
this.$nuxt.$on('pushState', params => {
history.pushState(
{},
null,
'search?phrase=' + encodeURIComponent(params)
)
})
},
beforeDestroy() {
this.$nuxt.$off('pushState')
},
methods: {
search() {
this.$nuxt.$emit('pushState', this.phrase)
}
},
However, all I have managed to achieve is to change the URL, but no redirection is happening. So what are the steps necessary to make this redirection to the mainURL + 'search?phrase=' + query so that user would be indeed redirected, and the items were displayed like right now.
In the /search/index.vue items are fetched and displayed, but how do I force router to go to this URL not only display it in the browser?

Testcafe - How do you wait for the `before` hook to complete before loading the page

I am using the test.before hook. My understanding is that this would be completed before the test would load the page.
What I am required to do is a navigateTo as the first action in my test.
test.page("/home").before(async t => {
await setupMockApis()
})("The bank account is added", async t => {
// the mock APIs are not finished setting up
// so I am required to do a navigateTo first
t.navigateTo("/home");
});
Is this the expected behaviour? Is it possible to get the before to complete before the test loads the page?
The common scenario is to login in before hook and it is implied that the page is loaded. In your case, you can avoid unnecessary page load omitting the page function call and navigating to your page in the hook as follows:
test.before(async t => {
await setupMockApis();
await t.navigateTo("/home");
})("The bank account is added", async t => {
});

How to POST data to a server in Sencha Touch without using AJAX

I am using Sencha Touch, I need post some data to a Server with a simple HTTP POST (NO AJAX)
At the moment I use
Ext.data.JsonP.request
Ext.Ajax.request
for my understanding both work with AJAX.
I would like to know how to disable the AJAX functionality and allow me to send some paramenters only via HTTP without using xhr and ajax.
You can create a form panel and then call the submit method with a url for the form to be submitted to:
// define your form
var form = Ext.create('Ext.form.Panel', {
...
// your form fields, etc
});
// later, in some handler for a button click, etc
form.submit({
url: 'url/to/submit.php',
method: 'POST',
success: function() {
// handle successful form submit
},
failure: funciton() { ... }
});

Ext.data.JsonP.request timeout

I have written a controller. When I click the button from view, this controller generic 'button' is called as follows:
'button': {
tap: function() {
Ext.data.JsonP.request({
url: 'http://download.finance.yahoo.com/d/quotes.csv',
params: {
s: '^BSESN',
f: 'nsl1op'
},
callbackKey: 'callback',
scope: this,
success: function( res, req ) {
Ext.example.msg('Sucess!', 'CSV file successfully generated.');
Ext.data.StoreManager.get('Files').load();
},
failure: function( res, req ) {
console.log('Failed to load csv file.');
}
});
....
....
....
It timesout and failure is called "Failed to load csv file."
The original URL I am using is "http://download.finance.yahoo.com/d/quotes.csv?s=^BSESN&f=nsl1op".
I would like know where I am going wrong.
You're requesting a CSV file via JSONP.
The file will be injected into the DOM via a '' tag (which is how JSONP works), but since it's not a valid JavaScript file, your callback (callbackKey: 'callback') is never executed, so Sencha Touch will fire the timeout handler, seeing the callback has not been fired by the injected <script> tag.
You probably need to change the URL to something that is actually JSONP (iow valid JavaScript wrapped in a callback), not a CSV file.

How to hide templates with AngularJS ngView for unauthorized users?

I have a basic PHP app, where the user login is stored in the HTTP Session. The app has one main template, say index.html, that switch sub-view using ngView, like this
<body ng-controller='MainCtrl'>
<div ng-view></div>
</body>
Now, this main template can be protected via basic PHP controls, but i have sub-templates (i.e. user list, add user, edit user, etc.) that are plain html files, included from angular according to my route settings.
While i am able to check for auth what concern the request of http services, one user is able to navigate to the sub-template url and access it. How can i prevent this from happen?
I would create a service like this:
app.factory('routeAuths', [ function() {
// any path that starts with /template1 will be restricted
var routeAuths = [{
path : '/template1.*',
access : 'restricted'
}];
return {
get : function(path) {
//you can expand the matching algorithm for wildcards etc.
var routeAuth;
for ( var i = 0; i < routeAuths.length; i += 1) {
routeAuth = routeAuths[i];
var routeAuthRegex = new RegExp(routeAuth.path);
if (routeAuthRegex.test(path)) {
if (routeAuth.access === 'restricted') {
return {
access : 'restricted',
path : path
};
}
}
}
// you can also make the default 'restricted' and check only for 'allowed'
return {
access : 'allowed',
path : path
};
}
};
} ]);
And in the main/root controller listen for $locationChangeStart events:
app.controller('AppController', ['$scope', '$route', '$routeParams', '$location', 'routeAuths',
function(scope, route, routeParams, location, routeAuths) {
scope.route = route;
scope.routeParams = routeParams;
scope.location = location;
scope.routeAuth = {
};
scope.$on('$locationChangeStart', function(event, newVal, oldVal) {
var routeAuth = routeAuths.get(location.path());
if (routeAuth.access === 'restricted') {
if (scope.routeAuth.allowed) {
event.preventDefault();
}
else {
//if the browser navigates with a direct url that is restricted
//redirect to a default
location.url('/main');
}
scope.routeAuth.restricted = routeAuth;
}
else {
scope.routeAuth.allowed = routeAuth;
scope.routeAuth.restricted = undefined;
}
});
}]);
Demo:
plunker
References:
angularjs services
location
UPDATE:
In order to fully prevent html template access then it's best done on the server as well. Since if you serve the html from a static folder on server a user can access the file directly ex: root_url/templates/template1.html thus circumventing the angular checker.
If you want to block them from going to that page create a service: http://docs.angularjs.org/guide/dev_guide.services.creating_services
This service can be dependency injected by all your controllers that you registered with the routeParams.
In the service you can would have a function that would check to see if the person is logged in or not and then re-route them (back to the login page perhaps?) using http://docs.angularjs.org/api/ng.$location#path. Call this function in each of the controllers like so:
function myController(myServiceChecker){
myServiceChecker.makeSureLoggedIn();
}
The makeSureLoggedIn function would check what current url they're at (using the $location.path) and if it's not one they're allowed to, redirect them back to a page that they are allowed to be.
I'd be interested to know if there's a way to prevent the routeParams from even firing, but at least this will let you do what you want.
Edit: Also see my answer here, you can prevent them from even going to the page:
AngularJS - Detecting, stalling, and cancelling route changes