Add RBL.urlFor in preparation for using Rails routes with Ajax calls

pull/6827/head
Mark Maglana 15 years ago
parent ef1d300656
commit 932515b0cf
  1. 24
      assets/javascripts/backlog.js
  2. 27
      assets/javascripts/item.js
  3. 18
      assets/javascripts/main.js

@ -193,10 +193,12 @@ RBL.Backlog = Class.create(RBL.Model, {
getChart: function(){ getChart: function(){
var div = this.getChild('.chart_area'); var div = this.getChild('.chart_area');
new Ajax.Updater(div, '/backlogs/' + this.getValue('.id') + '/chart?src=gchart', { var url = RBL.urlFor({ controller: 'charts',
method: 'get', action : 'show',
evalScripts: true 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(){ load: function(){
if(this.isMainBacklog()) return true; if(this.isMainBacklog()) return true;
var url = RBL.urlFor({ controller: 'backlogs',
action : 'show',
id : this.getValue('.id') });
this.showSpinner(); this.showSpinner();
new Ajax.Request('/backlogs/' + this.getValue('.id'), { new Ajax.Request(url, {
method : "get", method : "get",
onComplete: this.processDataFromServer.bind(this) onComplete: this.processDataFromServer.bind(this)
}); });
@ -365,11 +371,13 @@ RBL.Backlog = Class.create(RBL.Model, {
save: function(){ save: function(){
var params = this.toParams(); var params = this.toParams();
params["_method"] = "put"; var url = RBL.urlFor({ controller: 'backlogs',
action : 'update',
id : this.getValue('.id') });
this.showSpinner(); this.showSpinner();
new Ajax.Request('/backlogs/' + this.getValue('.id'), { new Ajax.Request(url, {
method : "post", method : "put",
parameters: params, parameters: params,
onComplete: this.processDataFromServer.bind(this) onComplete: this.processDataFromServer.bind(this)
}); });

@ -225,7 +225,11 @@ RBL.Item = Class.create(RBL.Model, {
}, },
loadComments: function(){ 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", method : "get",
onComplete: this.commentsLoaded.bind(this) onComplete: this.commentsLoaded.bind(this)
}); });
@ -234,7 +238,12 @@ RBL.Item = Class.create(RBL.Model, {
loadTasks: function(){ loadTasks: function(){
this.getTasksList().addClassName("loading"); 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", method : "get",
onComplete: this.tasksLoaded.bind(this) onComplete: this.tasksLoaded.bind(this)
}); });
@ -258,7 +267,7 @@ RBL.Item = Class.create(RBL.Model, {
save: function(saveCallback){ save: function(saveCallback){
var params = this.toParams(); var params = this.toParams();
var url = '/items'; var url;
var callback = null; var callback = null;
this._saveCallback = saveCallback; this._saveCallback = saveCallback;
@ -266,9 +275,13 @@ RBL.Item = Class.create(RBL.Model, {
if(this.isNew()){ if(this.isNew()){
params["project_id"] = projectID; params["project_id"] = projectID;
callback = this.itemCreated.bind(this); callback = this.itemCreated.bind(this);
url = RBL.urlFor({ controller: 'items',
action : 'create' });
} else { } else {
params["_method"] = "put"; params["_method"] = "put";
url += '/' + this.getValue('.id'); url = RBL.urlFor({ controller: 'items',
action : 'update',
id : this.getValue('.id') });
callback = this.itemUpdated.bind(this); callback = this.itemUpdated.bind(this);
} }
@ -280,7 +293,11 @@ RBL.Item = Class.create(RBL.Model, {
if(event.keyCode==Event.KEY_RETURN && event.ctrlKey) { if(event.keyCode==Event.KEY_RETURN && event.ctrlKey) {
var params = {}; var params = {};
params["comment"] = this.getChild("textarea.comment").value; 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', method : 'post',
parameters: params, parameters: params,
onComplete: this.commentSaved.bind(this) onComplete: this.commentSaved.bind(this)

@ -98,6 +98,24 @@ RBL.storePreferences = function(){
"expires=" + expiration.toGMTString(); "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<keys.length-1) url += "&";
});
return url;
}
/*************************************** /***************************************
BASE CLASS BASE CLASS
***************************************/ ***************************************/

Loading…
Cancel
Save