From 932515b0cf1331e96fe03af57ca80753636c29aa Mon Sep 17 00:00:00 2001 From: Mark Maglana Date: Thu, 20 Aug 2009 16:27:10 +0800 Subject: [PATCH] Add RBL.urlFor in preparation for using Rails routes with Ajax calls --- assets/javascripts/backlog.js | 24 ++++++++++++++++-------- assets/javascripts/item.js | 27 ++++++++++++++++++++++----- assets/javascripts/main.js | 18 ++++++++++++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/assets/javascripts/backlog.js b/assets/javascripts/backlog.js index 777e24a7d3..06617baee9 100644 --- a/assets/javascripts/backlog.js +++ b/assets/javascripts/backlog.js @@ -193,10 +193,12 @@ RBL.Backlog = Class.create(RBL.Model, { getChart: function(){ var div = this.getChild('.chart_area'); - new Ajax.Updater(div, '/backlogs/' + this.getValue('.id') + '/chart?src=gchart', { - method: 'get', - evalScripts: true - }); + var url = RBL.urlFor({ controller: 'charts', + action : 'show', + backlog_id: this.getValue('.id'), + src : 'gchart' }); + + new Ajax.Updater(div, url, { method: 'get', evalScripts: true }); }, @@ -319,8 +321,12 @@ RBL.Backlog = Class.create(RBL.Model, { load: function(){ if(this.isMainBacklog()) return true; + var url = RBL.urlFor({ controller: 'backlogs', + action : 'show', + id : this.getValue('.id') }); + this.showSpinner(); - new Ajax.Request('/backlogs/' + this.getValue('.id'), { + new Ajax.Request(url, { method : "get", onComplete: this.processDataFromServer.bind(this) }); @@ -365,11 +371,13 @@ RBL.Backlog = Class.create(RBL.Model, { save: function(){ var params = this.toParams(); - params["_method"] = "put"; + var url = RBL.urlFor({ controller: 'backlogs', + action : 'update', + id : this.getValue('.id') }); this.showSpinner(); - new Ajax.Request('/backlogs/' + this.getValue('.id'), { - method : "post", + new Ajax.Request(url, { + method : "put", parameters: params, onComplete: this.processDataFromServer.bind(this) }); diff --git a/assets/javascripts/item.js b/assets/javascripts/item.js index 340851f327..592db4c390 100644 --- a/assets/javascripts/item.js +++ b/assets/javascripts/item.js @@ -225,7 +225,11 @@ RBL.Item = Class.create(RBL.Model, { }, loadComments: function(){ - new Ajax.Request('/items/' + this.getValue('.id') + '/comments', { + var url = RBL.urlFor({ controller: 'comments', + action : 'index', + item_id : this.getValue('.id') }); + + new Ajax.Request(url, { method : "get", onComplete: this.commentsLoaded.bind(this) }); @@ -234,7 +238,12 @@ RBL.Item = Class.create(RBL.Model, { loadTasks: function(){ this.getTasksList().addClassName("loading"); - new Ajax.Request('/items/' + this.getValue('.id') + '/tasks', { + + var url = RBL.urlFor({ controller: 'tasks', + action : 'index', + item_id : this.getValue('.id') }); + + new Ajax.Request(url, { method : "get", onComplete: this.tasksLoaded.bind(this) }); @@ -258,7 +267,7 @@ RBL.Item = Class.create(RBL.Model, { save: function(saveCallback){ var params = this.toParams(); - var url = '/items'; + var url; var callback = null; this._saveCallback = saveCallback; @@ -266,9 +275,13 @@ RBL.Item = Class.create(RBL.Model, { if(this.isNew()){ params["project_id"] = projectID; callback = this.itemCreated.bind(this); + url = RBL.urlFor({ controller: 'items', + action : 'create' }); } else { params["_method"] = "put"; - url += '/' + this.getValue('.id'); + url = RBL.urlFor({ controller: 'items', + action : 'update', + id : this.getValue('.id') }); callback = this.itemUpdated.bind(this); } @@ -280,7 +293,11 @@ RBL.Item = Class.create(RBL.Model, { if(event.keyCode==Event.KEY_RETURN && event.ctrlKey) { var params = {}; params["comment"] = this.getChild("textarea.comment").value; - new Ajax.Request('/items/' + this.getValue('.id') + '/comments', { + var url = RBL.urlFor({ controller: 'comments', + action : 'create', + item_id : this.getValue('.id') }); + + new Ajax.Request(url, { method : 'post', parameters: params, onComplete: this.commentSaved.bind(this) diff --git a/assets/javascripts/main.js b/assets/javascripts/main.js index 1774ac6e61..76a4c129ff 100644 --- a/assets/javascripts/main.js +++ b/assets/javascripts/main.js @@ -98,6 +98,24 @@ RBL.storePreferences = function(){ "expires=" + expiration.toGMTString(); } + +RBL.urlFor = function(options){ + // THINKABOUTTHIS: Is it worth using Rails' routes for this instead? + var url = '/' + options['controller'] + if(options['action']!=null && options['action'].match(/index/)==null) url += '/' + options['action']; + if(options['id']!=null) url += "/" + options['id']; + + var keys = Object.keys(options).select(function(key){ return key!="controller" && key!="action" && key!="id" }); + if(keys.length>0) url += "?"; + + keys.each(function(key, index){ + url += key + "=" + options[key]; + if(index