lowpro autosave behavior to POST JSON
December 20th, 2007 • Uncategorized
Event.addBehavior({
'.autosave': Behavior.create({
onchange : function() {
var source = this.element;
var name = source.name.split('['); // name according to rails convention
var model = name[0]; // rails model name
var resource = model + 's'; // rails resource (TODO better pluralization)
var id = name[1].substring(0, name[1].length - 1); // rails model id
var authToken = 'authenticity_token=' + $('auth-token').getValue()
if(name[1].match(/new/)) {
var url = '/' + resource + '?' + authToken
} else {
var url = '/' + resource + '/' + id + '?_method=put&' + authToken;
}
var inputs = $(source.parentNode).id.split('-');
new Ajax.Request(url, {
method: 'post',
contentType: "application/json",
postBody: Object.toJSON({
survey_execution_id: $('survey_execution_id').getValue(),
question_application_input_id: inputs[1],
time_line_offset: inputs[2],
value: source.getValue()
})
});
}
});
});