# HG changeset patch
# User Anton Shestakov <engored@ya.ru>
# Date 1357892545 -32400
# Node ID fe92472b29b77d2465f331ede41a76184f9825a6
# Parent  ccb4fca3d43aa4b2a7911e08f787b7675f38d009
Fetching projects and tasks all at once.

diff -r ccb4fca3d43a -r fe92472b29b7 app.py
--- a/app.py	Fri Jan 11 15:19:57 2013 +0900
+++ b/app.py	Fri Jan 11 17:22:25 2013 +0900
@@ -8,7 +8,7 @@
 from CodernityDB.database_thread_safe import ThreadSafeDatabase
 
 from fruitbar.indexes import ProjectIndex, TaskIndex
-from fruitbar.crud import ResourceList, CRUDResource
+from fruitbar.crud import ResourceList, ResourceCombiner, CRUDResource
 
 
 DEBUG = True
@@ -69,11 +69,19 @@
     safe_fields = ('name', 'note', 'done')
 
 
+class ProjectsAndTasks(ResourceCombiner):
+    combine = {
+        'projects': ProjectList,
+        'tasks': TaskList
+    }
+
+
 api = Api(app)
 api.add_resource(ProjectList, '/<workspace>/projects/')
 api.add_resource(Project, '/<workspace>/projects/<resource_id>/')
 api.add_resource(TaskList, '/<workspace>/tasks/')
 api.add_resource(Task, '/<workspace>/tasks/<resource_id>/')
+api.add_resource(ProjectsAndTasks, '/<workspace>/all/')
 
 
 if __name__ == '__main__':
diff -r ccb4fca3d43a -r fe92472b29b7 fruitbar/crud.py
--- a/fruitbar/crud.py	Fri Jan 11 15:19:57 2013 +0900
+++ b/fruitbar/crud.py	Fri Jan 11 17:22:25 2013 +0900
@@ -22,6 +22,19 @@
         return g.db.get('id', response['_id'], with_doc=True)
 
 
+class ResourceCombiner(Resource):
+    combine = {}
+    
+    def get(self, workspace):
+        result = {}
+        
+        for key, resource_class in self.combine.items():
+            resource = resource_class()
+            result[key] = resource.get(workspace)
+        
+        return result
+
+
 class CRUDResource(Resource):
     """ CRUD? More like RUD!
     """
diff -r ccb4fca3d43a -r fe92472b29b7 static/js/ui.js
--- a/static/js/ui.js	Fri Jan 11 15:19:57 2013 +0900
+++ b/static/js/ui.js	Fri Jan 11 17:22:25 2013 +0900
@@ -41,15 +41,27 @@
         this.length = 0;
     };
     
+    fruitbar.fetchAll = function(url) {
+        return Backbone.ajax({
+            url: url,
+            type: 'GET',
+            dataType: 'json'
+        }).done(function(data) {
+            fruitbar.projects.update(data['projects']);
+            fruitbar.tasks.update(data['tasks']);
+        });
+    };
+    
     fruitbar.on('workspace', function(workspace) {
         $('body').stop().animate({opacity: 0});
         
         this.xhrs.abort();
         
+        this.workspace = workspace;
         this.tasks.url = '/' + encodeURIComponent(workspace) + '/tasks/';
         this.projects.url = '/' + encodeURIComponent(workspace) + '/projects/';
         
-        this.xhrs.push(this.tasks.fetch(), this.projects.fetch());
+        this.xhrs.push(this.fetchAll('/' + encodeURIComponent(workspace) + '/all/'));
         
         _(this.xhrs).each(function(fetch, index, xhrs) {
             fetch.then(function() {
@@ -64,11 +76,8 @@
     
     fruitbar.once('workspace', function() {
         window.setInterval(function() {
-            fruitbar.xhrs.push(fruitbar.tasks.fetch({update: true}));
-        }, 60000);
-        window.setInterval(function() {
-            fruitbar.xhrs.push(fruitbar.projects.fetch({update: true}));
-        }, 60000);
+            fruitbar.xhrs.push(fruitbar.fetchAll('/' + encodeURIComponent(fruitbar.workspace) + '/all/'));
+        }, 6000);
     });
     
     Backbone.history.start();