I am checking to see if a url contains the word "rewrite", if it does I am adding a cookie. However, I want to reload the page after the cookie is added, then stop reloading the page. Currently it keeps reloading the page because it sees the url and it contains the word "rewrite". Is there like a Do Once in jQuery or a Stop() method? Perhpas I have to use .live()?
$(document).ready(function () {
if ($.cookie('logged_in') == null) {
Do Nothing
} else {
Do Something
}
});
$(document).ready(function () {
if (/rewrite/.test(self.location.href)) {
$.cookie("logged_in", 1, { expires : 1 });
location.reload(); stop();
}
});
SOLVED
if (/rewrite/.test(self.location.href)) {
if ($.cookie('logged_in') == null) {
$.cookie("logged_in", 1, { expires : 1 });
location.reload();
}
Check for rewrite in the URL AND the cookie, if both then you know you've already done the reload?
Related
I want to know how can i call POST web service method using javascript, when i click on button in ORACLE APEX.
Thank you for help
var url = "your_url";
xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4 && xhr.status == 200) {
//do whatever you want to do
} else {
//do whatever you want to do
}
}
xhr.send(JSON.stringify({
"key_1": "value_1",
"key_2": "value_2"
}));
Hope this will help you.
I am trying to use alertify confirmation box when someone tries to navigate from the page. But in the willTransition action of the route, alertify is very asynchronous, and ember doesn't wait for the confirmation. No matter what you click, the page is already navigated.
willTransition(transition) {
alertify.confirm('Confirm', 'Are you sure you want to navigate?', function(e) {
if(e) {
return true;
} else {
transition.abort();
}
});
}
Please help me with this!!
You can abort and retry transitions. You have to abort the transition before showing the confirmation dialog. After confirming your dialog you can retry the transition and prevent your code from showing your confirmation dialog again. So the following should work (not tested):
export default Ember.Route.extend({
// ...
showConfirmation: true,
actions: {
willTransition(transition) {
if(!this.get('showConfirmation')) {
this.set('showConfirmation', true);
return true;
}
// Abort the transition for now
transition.abort();
// Now show a confirm box with alertify.
// According to the docs, only the following
// is allowed: http://alertifyjs.com/confirm.html
alertify.confirm('Are you sure you want to navigate?', () => {
// According to the documentation of alertify,
// this code is only run when you confirm your box.
this.set('showConfirmation', false);
transition.retry();
});
}
}
}
Just trying to figure things out in Meteor JS. I am stuck trying to get a Meteor.call result object passed back into a template. Here is my code:
HTML Template
<template name="showTotals">
<div class="container">
{{#each recordCnt}}
{{/each}}
</div>
</template>
Client JS
//get a count per item
Template.showTotals.recordCnt = function(){
Meteor.call('getCounts', function(err, result) {
if (result) {
//console.log(result); <-this shows object in javascript console
return result;
}
else {
console.log("blaaaa");
}
});
}
Server JS
Meteor.startup(function () {
Meteor.methods({
getCounts: function() {
var pipeline = [{$group: {_id: null, count: {$sum : 1}}}];
count_results = MyCollection.aggregate(pipeline);
return count_results;
}
});
});
Global JS
MyCollection = new Meteor.Collection('folks');
I have tried adding a console.log(result) for the results of the call, and it displays in the javascript console with the expected results. I cannot get this to populate recordCnt however.
Any ideas?
You can't do it the way you want because the callback from the Meteor.call runs asynchronously.
You can, however, use the callback to set a Session value and have your helper read Session.get() instead.
Template.showTotals.rcdCount = function() {
return Session.get('rcdCount');
}
Template.showTotals.rendered = function() {
Meteor.call('getCounts', function(err, result) {
if (result) {
Session.set('rcdCount', result);
}
else {
console.log("blaaaa");
}
});
}
See here for a similar answer that includes an example of a "hacky" way of doing it.
I have a django backed angular app that uses angular-route to resolve the urls
I have a login system that users a factory to verify the authorisation of a user, as mentioned here.
Here is a part of my app.js file
.factory('Auth', function (){
var user;
return {
setUser : function (u)
{
user = u;
},
isLoggedIn : function()
{
var User = false;
return User;
}
}
}
)
.config(
function($routeProvider)
{
$routeProvider
.when('/login', {
templateUrl : '/login',
controller:'LoginCtrl'
})
.otherwise('/')
}
)
.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
$rootScope.$on('$routeChangeStart', function (event) {
if (!Auth.isLoggedIn()) {
console.log('DENY');
event.preventDefault();
$location.path('/login');
}
else {
console.log('ALLOW');
$location.path('/home');
}
});
}])
I also have a watch set in my MainCtrl.
$scope.$watch(Auth.isLoggedIn,
function(value)
{
if(!value)
{
$location.path('/login')
}
else
{
$location.path('/home')
}
})
My issue is that, the /login template is never requested, even if I manually try /#/login, no errors in console as well.
Also, the location.$path('/login') is also not executed, as page stays right at requested
URL. However, DENY gets printed in the console log.
I suspect that it is due to the app.run attribute , as the template gets rendered if I remove the run attribute.
It is simple : "login" is a state, and you forbid access to any state if the user is not logged in !
You could just change this for example :
.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
$rootScope.$on('$routeChangeStart', function (event, toState, fromState) {
if (!Auth.isLoggedIn() && toState == 'login' ){
console.log('ALLOW');
}
else if (!Auth.isLoggedIn() && toState != 'login') {
console.log('DENY');
event.preventDefault();
location.path('/login');
}
else {
console.log('ALLOW');
$location.path('/home');
}
});
Also if you want to see a very complete auth system in angular.js, have a glance at this :
https://github.com/angular-app/angular-app/tree/master/client/src/common/security
If I'm logged into facebook the testAPI() function fires as it's supposed to, but when I log out of facebook and refresh the page where my init code is, nothing happens. My console should log "not logged in", or "not authorized", but it doesn't. I've tried clearing the cache, but it doesn't work.
FB.Event.subscribe('auth.authResponseChange', function(response) {
//this works
testAPI();
} else if (response.status === 'not_authorized') {
console.log("not authorized");
} else {
console.log("not logged in");
}
});
var testAPI = function() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
};
auth.authResponseChange only fires when the status actually change, in your case it never change.
What you want is to use FB.getLoginStatus.