Add path prefix to all AJAX requests

This will handle the case where the site was deployed under a
path prefix.
This commit is contained in:
Prem Sichanugrist
2012-05-14 16:32:27 -04:00
parent 80ba9bd0ff
commit 2bc68c0f25
12 changed files with 23 additions and 15 deletions

View File

@@ -20,5 +20,5 @@ class @Calculator
$('.help').toggleClass 'shown'
calculate: ->
$.getJSON '/calculate', { equation: $('#calculator_input').val() }, (data) ->
$.getWithPrefix '/calculate', { equation: $('#calculator_input').val() }, (data) ->
$('#calculator_output').val(data.result)

View File

@@ -1,5 +1,6 @@
class @Courseware
constructor: ->
Courseware.prefix = $("meta[name='path_prefix']").attr('content')
new Navigation
new Calculator
new FeedbackForm

View File

@@ -5,6 +5,6 @@ class @FeedbackForm
subject: $('#feedback_subject').val()
message: $('#feedback_message').val()
url: window.location.href
$.post '/send_feedback', data, ->
$.postWithPrefix '/send_feedback', data, ->
$('#feedback_div').html 'Feedback submitted. Thank you'
,'json'

View File

@@ -1,3 +1,9 @@
jQuery.postWithPrefix = (url, data, callback, type) ->
$.post("#{Courseware.prefix}#{url}", data, callback, type)
jQuery.getWithPrefix = (url, data, callback, type) ->
$.get("#{Courseware.prefix}#{url}", data, callback, type)
$ ->
$.ajaxSetup
headers : { 'X-CSRFToken': $.cookie 'csrftoken' }
@@ -18,8 +24,8 @@ $ ->
element.schematic.update_value()
schematic_value $("#schematic_#{circuit_id}").attr("value")
$.post "/save_circuit/#{circuit_id}", schematic: schematic_value, (data) ->
$.postWithPrefix "/save_circuit/#{circuit_id}", schematic: schematic_value, (data) ->
alert('Saved') if data.results == 'success'
window.postJSON = (url, data, callback) ->
$.post url, data, callback
$.postWithPrefix url, data, callback