Download:
child 25:79ae5c1900a2
parent 23:ccb4fca3d43a
24:fe92472b29b7
Anton Shestakov <engored@ya.ru>, Fri, 11 Jan 2013 17:22:25 +0900
Fetching projects and tasks all at once.

3 файлов изменено, 37 вставок(+), 7 удалений(-) [+]
app.py file | annotate | diff | comparison | revisions
fruitbar/crud.py file | annotate | diff | comparison | revisions
static/js/ui.js file | annotate | diff | comparison | revisions
--- 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__':
--- 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!
"""
--- 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();