Advertisement
thebullno1

Untitled

Nov 18th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     "use strict";
  3.  
  4.     var token = storage.get("token");
  5.     var isAdmin = false;
  6.     var username = null;
  7.  
  8.     function call(method, params) {
  9.         params = params || {};
  10.         //Retrieve the callbacks
  11.         var onSuccess = params.onSuccess || noop;
  12.         var onFailure = params.onFailure || noop;
  13.         var onFinish = params.onFinish || noop;
  14.  
  15.         if(token) params.token = token;
  16.  
  17.         //Remove them from this object since we will just reuse it later
  18.         delete params['onFinish'];
  19.         delete params['onFailure'];
  20.         delete params['onSuccess'];
  21.  
  22.         //Call to our server
  23.         $.ajax({
  24.             url: '/api/' + method,
  25.             cache: false,
  26.             dataType: 'json',
  27.             data: params,
  28.             complete: onFinish,
  29.             type: 'POST',
  30.             success: function(data) {
  31.                 if(data.success) {
  32.                     onSuccess(data.result);
  33.                 }
  34.                 else {
  35.                     onFailure(data);
  36.                 }
  37.             },
  38.             error: function() {
  39.                 onFailure({
  40.                     error: "net_err",
  41.                     desc: "Network error"
  42.                 });
  43.             }
  44.         });
  45.     }
  46.  
  47.     function setToken(value) {
  48.         token = value;
  49.         storage.set('token', value);
  50.     }
  51.  
  52.     function clearToken() {
  53.         token = null;
  54.         storage.set('token', null);
  55.     }
  56.  
  57.     function noop() {}
  58.  
  59.     function addTrivialFunction(name) {
  60.         window.nomnom[name] = function(params) {
  61.             return call(name, params);
  62.         }
  63.     }
  64.  
  65.     var trivialFunctions = [
  66.         'search',
  67.         'view_place',
  68.         'view_menu',
  69.         'view_food',
  70.         'pending_places',
  71.         'pending_foods',
  72.         'approve_place',
  73.         'reject_place',
  74.         'approve_food',
  75.         'reject_food',
  76.         'suggest_place',
  77.         'suggest_food',
  78.         'edit_place',
  79.         'edit_food',
  80.         'remove_place',
  81.         'remove_food',
  82.         'add_to_menu',
  83.         'remove_from_menu',
  84.         'edit_menu',
  85.         'rate',
  86.         'post_review',
  87.         'view_reviews',
  88.         'suggested_places',
  89.         'suggested_foods',
  90.         'view_profile',
  91.         'similar_foods'
  92.     ];
  93.  
  94.     //To export functions from a module, just return an object that includes them
  95.     window.nomnom = {
  96.         //currently, login is quite simple, in the future, we can save the token in the browser after a successful login
  97.         login: function(params) {
  98.             params.password = hex_md5(params.password);
  99.             var userOnSuccess = params.onSuccess || noop;
  100.             params.onSuccess = onSuccess;
  101.             call('login', params);
  102.  
  103.             function onSuccess(result) {
  104.                 username = params.username;
  105.                 setToken(result.token);
  106.                 userOnSuccess(result);
  107.             }
  108.         },
  109.  
  110.         register: function(params) {
  111.             params.password = hex_md5(params.password);
  112.             call('register', params);
  113.         },
  114.         logout: function(params) {
  115.             clearToken();
  116.             call('logout', params);
  117.         },
  118.         isLoggedIn: function() {
  119.             return token ? true:false;
  120.         }
  121.     }
  122.  
  123.     for(var i = 0; i < trivialFunctions.length; ++i)
  124.         addTrivialFunction(trivialFunctions[i]);
  125. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement