Client side script seems to not work on page change
-
I have currently a client side code that redirects users if they are not logged in to the log in page so that non-members can't view the pages. The code is as follows.
$(window).on('action:ajaxify.end', function(ev, data) { if(data.url !== 'login' && data.url !== 'register'){ if (!app.user.uid || app.user.uid === 0) { ajaxify.go('login'); } } });
The code works on the initial page load, but if I click a different page from the login page like going to the categories page, then it no longer works. I need to refresh the page in order for it to work.
I have noticed that if I put an alert or try debugging it always works. Perhaps this is a dumb question on my part but do you guys have any ideas as to why this is the case and how do I fix it to work on page change as well? -
@baris I seem to have the same issues. And the browser console seems to come up blank for errors. I've also tested on multiple browsers to see if it was a just a browser specific issue but that doesn't seem to be the case. I actually got a very botched way of making it work currently with the code below.
$(window).on('action:ajaxify.end', function(ev, data) { if (data.url !== 'login' && data.url !== 'register' && !app.user.uid) { ajaxify.go('login'); } setTimeout(function (){ if (data.url !== 'login' && data.url !== 'register' && !app.user.uid) { ajaxify.go('login'); } }, 500); });
I just do the function again after a slight delay and it seems to be working. Which is really odd and not ideal.
-
You can add a console.log and see if it is getting triggered at all.
$(window).on('action:ajaxify.end', function(ev, data) { console.log('action:ajaxify.end'); if(ajaxify.data.template.name !== 'login' && ajaxify.data.template.name !== 'register'){ if (!app.user.uid || app.user.uid === 0) { ajaxify.go('login'); } } });
-
So it is always going into the
action:ajaxify.end
handler? Can you try the below and see if it works?$(window).on('action:ajaxify.end', function(ev, data) { console.log('action:ajaxify.end'); if(ajaxify.data.template.name !== 'login' && ajaxify.data.template.name !== 'register'){ if (!app.user.uid || app.user.uid === 0) { $('#content, #footer').removeClass('ajaxifying'); ajaxify.go('login'); } } });
-
@baris This specific function is currently on a mostly blank slate, which makes it more confusing as well. I've tried putting in an console log after each if statement as well and it all goes through. It even goes to the
ajaxify.go('login')
but that just seems to do nothing.