Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

dashboard.js

By: handoyo on Nov 11th, 2013  |  syntax: JavaScript  |  size: 2.31 KB  |  views: 105  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. $('#page_dashboard').on('pageshow',function(){
  2. try{
  3. $.ajax({
  4.   url:"http://your_url/services/session/token",
  5.   dataType:"text",
  6.   error:function (jqXHR, textStatus, errorThrown) {
  7.     alert(jqXHR);
  8.     alert(textStatus);
  9.   },
  10.   success: function (token) {
  11.         // Call system connect with session token.
  12.         $.ajax({
  13.       url: 'http://your_url/drupal/endpoint/system/connect.json',
  14.       type: "post",
  15.       dataType: "json",
  16.       beforeSend: function (request) {
  17.         request.setRequestHeader("X-CSRF-Token", token);
  18.       },
  19.       error: function (jqXHR, textStatus, errorThrown) {
  20.       alert(jqXHR);
  21.       alert(textStatus);
  22.       //  alert(errorThrown);
  23.       },
  24.       success: function (data) {
  25.        var drupal_user = data.user;
  26.        if (drupal_user.uid == 0) { // user is not logged in, show the login button, hide the logout button
  27.                  $('#button_login').show();
  28.                  $('#button_logout').hide();
  29.                }
  30.                else { // user is logged in, hide the login button, show the logout button
  31.                  $('#button_login').hide();
  32.                  $('#button_logout').show();
  33.                }
  34.       }
  35.     });
  36.   }
  37. });
  38. }
  39. catch(error) { alert("page_dashboard - " + error)};
  40. });
  41.  
  42. $(document).on("click",'#button_logout',function(){
  43. try {
  44.         $.ajax({
  45.   url:"http://your_url/services/session/token",
  46.   type:"get",
  47.   dataType:"text",
  48.   error:function (jqXHR, textStatus, errorThrown) {
  49.    alert(jqXHR);
  50.    alert(textStatus);
  51.    // alert(errorThrown);
  52.   },
  53.   success: function (token) {
  54.         // Call system connect with session token.
  55.     $.ajax({
  56.  
  57.      url: "http://your_url/endpoint/user/logout.json",
  58.      type: 'post',
  59.      dataType: 'json',
  60.          beforeSend: function (request) {
  61.         request.setRequestHeader("X-CSRF-Token", token);
  62.       },
  63.      error: function (jqXHR, textStatus, errorThrown) {
  64.        alert('button_logout - failed to logout');
  65.        alert(JSON.stringify(jqXHR));
  66.        alert(JSON.stringify(textStatus));
  67.        alert(JSON.stringify(errorThrown));
  68.      },
  69.      success: function (data) {
  70.        alert("You have been logged out.");
  71.        $.mobile.changePage("index.html",{reloadPage:true},{allowSamePageTranstion:true},{transition:'none'});
  72.      }
  73.  });
  74.   }
  75. });
  76.  
  77. }
  78. catch (error) { alert("button_logout - " + error); }
  79. });