Browse Source

less hacky limit on api GET's

Rafał Pitoń 9 years ago
parent
commit
dc4589b2a6

+ 2 - 1
misago/frontend/misago/app.js

@@ -56,7 +56,8 @@
       this.addService('conf', ns.Conf);
       this.addService('conf', ns.Conf);
       this.addService('component', ns.ComponentFactory);
       this.addService('component', ns.ComponentFactory);
       this.addService('router', ns.RouterFactory);
       this.addService('router', ns.RouterFactory);
-      this.addService('api', ns.Api);
+      this.addService('ajax', ns.ajax);
+      this.addService('api', ns.api);
       this.addService('outlet', ns.Outlet);
       this.addService('outlet', ns.Outlet);
       this.addService('title', ns.PageTitle);
       this.addService('title', ns.PageTitle);
       this.addService('start-routing', ns.startRouting);
       this.addService('start-routing', ns.startRouting);

+ 1 - 5
misago/frontend/misago/routes/legal.js

@@ -16,16 +16,12 @@
       },
       },
       vm: {
       vm: {
         isReady: false,
         isReady: false,
-        isBusy: false,
         init: function(component, _) {
         init: function(component, _) {
           if (this.isReady) {
           if (this.isReady) {
             _.setTitle(this.title);
             _.setTitle(this.title);
           } else {
           } else {
             _.setTitle();
             _.setTitle();
-            if (!this.isBusy) {
-              this.isBusy = true;
-              return _.api.one('legal-pages', dashedTypeName);
-            }
+            return _.api.one('legal-pages', dashedTypeName);
           }
           }
         },
         },
         ondata: function(data, component, _) {
         ondata: function(data, component, _) {

+ 74 - 0
misago/frontend/misago/services/ajax.js

@@ -0,0 +1,74 @@
+(function (Misago) {
+  'use strict';
+
+  var Ajax = function(_) {
+    var cookieRegex = new RegExp(_.context.CSRF_COOKIE_NAME + '\=([^;]*)');
+    this.csrfToken = Misago.get(document.cookie.match(cookieRegex), 0).split('=')[1];
+
+    // list of gets underway
+    var runningGets = {};
+
+    this.ajax = function(method, url, data, progress) {
+      var promise = m.deferred();
+
+      var ajax_settings = {
+        url: url,
+        method: method,
+        headers: {
+          'X-CSRFToken': this.csrfToken
+        },
+
+        data: data | {},
+        dataType: 'json',
+
+        success: function(data) {
+          if (method === 'GET') {
+            Misago.pop(runningGets, url);
+          }
+          promise.resolve(data);
+        },
+        error: function(jqXHR) {
+          if (method === 'GET') {
+            Misago.pop(runningGets, url);
+          }
+
+          var rejection = jqXHR.responseJSON || {};
+
+          rejection.status = jqXHR.status;
+          rejection.statusText = jqXHR.statusText;
+
+          promise.reject(rejection);
+        }
+      };
+
+      if (progress) {
+        return; // not implemented... yet!
+      }
+
+      $.ajax(ajax_settings);
+      return promise.promise;
+    };
+
+    this.get = function(url) {
+      var preloaded = Misago.pop(_.context, url);
+      if (preloaded) {
+        var deferred = m.deferred();
+        deferred.resolve(preloaded);
+        return deferred.promise;
+      } else if (runningGets[url] !== undefined) {
+        return runningGets[url];
+      } else {
+        runningGets[url] = this.ajax('GET', url);
+        return runningGets[url];
+      }
+    };
+
+    this.post = function(url) {
+      return this.ajax('POST', url);
+    };
+  };
+
+  Misago.ajax = function(_) {
+    return new Ajax(_);
+  };
+}(Misago.prototype));

+ 2 - 56
misago/frontend/misago/services/api.js

@@ -2,60 +2,6 @@
   'use strict';
   'use strict';
 
 
   var Api = function(_) {
   var Api = function(_) {
-    // Ajax implementation
-    var cookie_regex = new RegExp(_.context.CSRF_COOKIE_NAME + '\=([^;]*)');
-    this.csrfToken = Misago.get(document.cookie.match(cookie_regex), 0).split('=')[1];
-
-    this.ajax = function(method, url, data, progress) {
-      var promise = m.deferred();
-
-      var ajax_settings = {
-        url: url,
-        method: method,
-        headers: {
-          'X-CSRFToken': this.csrfToken
-        },
-
-        data: data | {},
-        dataType: 'json',
-
-        success: function(data) {
-          promise.resolve(data);
-        },
-        error: function(jqXHR) {
-          var rejection = jqXHR.responseJSON || {};
-
-          rejection.status = jqXHR.status;
-          rejection.statusText = jqXHR.statusText;
-
-          promise.reject(rejection);
-        }
-      };
-
-      if (progress) {
-        return; // not implemented... yet!
-      }
-
-      $.ajax(ajax_settings);
-      return promise.promise;
-    };
-
-    this.get = function(url) {
-      var context = Misago.pop(_.context, url);
-      if (context) {
-        var deferred = m.deferred();
-        deferred.resolve(context);
-        return deferred.promise;
-      } else {
-        return this.ajax('GET', url);
-      }
-    };
-
-    this.post = function(url) {
-      return this.ajax('POST', url);
-    };
-
-    // API
     this.buildUrl = function(model, call, querystrings) {
     this.buildUrl = function(model, call, querystrings) {
       var url = _.router.baseUrl;
       var url = _.router.baseUrl;
       url += 'api/' + model + '/';
       url += 'api/' + model + '/';
@@ -64,7 +10,7 @@
 
 
     this.one = function(model, id) {
     this.one = function(model, id) {
       var url = this.buildUrl(model) + id + '/';
       var url = this.buildUrl(model) + id + '/';
-      return this.get(url);
+      return _.ajax.get(url);
     };
     };
 
 
     this.many = function(model, filters) {
     this.many = function(model, filters) {
@@ -76,7 +22,7 @@
     };
     };
   };
   };
 
 
-  Misago.Api = function(_) {
+  Misago.api = function(_) {
     return new Api(_);
     return new Api(_);
   };
   };
 }(Misago.prototype));
 }(Misago.prototype));

+ 3 - 6
misago/frontend/misago/services/route.js

@@ -22,7 +22,7 @@
       var __onunload = controller.onunload || noop;
       var __onunload = controller.onunload || noop;
       controller.onunload = function() {
       controller.onunload = function() {
         __onunload.apply(component, arguments);
         __onunload.apply(component, arguments);
-        controller.isActive = false;
+        component.isActive = false;
       };
       };
 
 
       return controller;
       return controller;
@@ -31,17 +31,14 @@
     // Add state callbacks to View-Model
     // Add state callbacks to View-Model
     if (component.vm && component.vm.init) {
     if (component.vm && component.vm.init) {
       // wrap vm.init in promise handler
       // wrap vm.init in promise handler
-      component.vm._promise = null;
-
       var __init = component.vm.init;
       var __init = component.vm.init;
       component.vm.init = function() {
       component.vm.init = function() {
         var initArgs = arguments;
         var initArgs = arguments;
         var promise = __init.apply(component.vm, initArgs);
         var promise = __init.apply(component.vm, initArgs);
 
 
         if (promise) {
         if (promise) {
-          component.vm._promise = promise;
           promise.then(function() {
           promise.then(function() {
-            if (component.vm._promise === promise && component.vm.ondata) {
+            if (component.isActive && component.vm.ondata) {
               var finalArgs = [];
               var finalArgs = [];
               for (var i = 0; i < arguments.length; i++) {
               for (var i = 0; i < arguments.length; i++) {
                 finalArgs.push(arguments[i]);
                 finalArgs.push(arguments[i]);
@@ -53,7 +50,7 @@
               component.vm.ondata.apply(component.vm, finalArgs);
               component.vm.ondata.apply(component.vm, finalArgs);
             }
             }
           }, function(error) {
           }, function(error) {
-            if (component.vm._promise === promise) {
+            if (component.isActive) {
               component.container.router.errorPage(error);
               component.container.router.errorPage(error);
             }
             }
           });
           });

+ 4 - 3
misago/frontend/misago/services/router.js

@@ -113,17 +113,18 @@
 
 
     // Errors
     // Errors
     this.error403 = function(error) {
     this.error403 = function(error) {
+      var component = null;
       if (error.ban) {
       if (error.ban) {
-        var component = routedComponent(Misago.ErrorBannedRoute);
+        component = routedComponent(Misago.ErrorBannedRoute);
       } else {
       } else {
-        var component = routedComponent(Misago.Error403Route);
+        component = routedComponent(Misago.Error403Route);
       }
       }
 
 
       component.vm = error;
       component.vm = error;
       m.mount(this.fixture, component);
       m.mount(this.fixture, component);
     };
     };
 
 
-    this.error404 = function(error) {
+    this.error404 = function() {
       m.mount(this.fixture, routedComponent(Misago.Error404Route));
       m.mount(this.fixture, routedComponent(Misago.Error404Route));
     };
     };