Commits

Anton Shestakov committed 39ac248

Use events to not smear workspaces all over the code.

Comments (0)

Files changed (3)

static/js/framework/collections.js

 var Projects = Backbone.Collection.extend({
-    model: Project,
-    url: function() {
-        return '/' + encodeURIComponent(fruitbar.workspace) + '/projects/';
-    }
+    model: Project
 });
 
 var Tasks = Backbone.Collection.extend({
-    model: Task,
-    url: function() {
-        return '/' + encodeURIComponent(fruitbar.workspace) + '/tasks/';
-    }
+    model: Task
 });

static/js/framework/router.js

     },
     
     setWorkspace: function(workspace) {
-        fruitbar.workspace = workspace;
-        
-        fruitbar.tasks.fetch();
-        fruitbar.projects.fetch();
+        fruitbar.trigger('workspace', workspace);
     }
 });
  $(function() {
-    window.fruitbar = {workspace: undefined};
+    window.fruitbar = new Backbone.View();
     fruitbar.tasks = new Tasks();
     fruitbar.projects = new Projects();
     fruitbar.router = new Router();
         }
     });
     
+    fruitbar.on('workspace', function(workspace) {
+        this.tasks.url = '/' + encodeURIComponent(workspace) + '/tasks/';
+        this.projects.url = '/' + encodeURIComponent(workspace) + '/projects/';
+        
+        this.tasks.fetch();
+        this.projects.fetch();
+    });
+    
+    fruitbar.once('workspace', function() {
+        window.setInterval(function() {fruitbar.projects.fetch({update: true});}, 60000);
+        window.setInterval(function() {fruitbar.tasks.fetch({update: true});}, 60000);
+    });
+    
     Backbone.history.start();
-    
-    window.setInterval(function() {fruitbar.projects.fetch({update: true});}, 60000);
-    window.setInterval(function() {fruitbar.tasks.fetch({update: true});}, 60000);
 });