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(){
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)
});

@ -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)

@ -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<keys.length-1) url += "&";
});
return url;
}
/***************************************
BASE CLASS
***************************************/

Loading…
Cancel
Save