Ruby on rails 为什么我的ajaxget请求不总是返回正确的值、RoR和CoffeeScript

Ruby on rails 为什么我的ajaxget请求不总是返回正确的值、RoR和CoffeeScript,ruby-on-rails,ajax,get,Ruby On Rails,Ajax,Get,我的咖啡脚本文件如下所示: updateDot = (iDraggingID, newX, newY) -> $.ajax "/dots/#{iDraggingID}", type:'PUT' dataType:'json' data: {dot: {position_x: "#{newX}", position_y: "#{newY}"}} refreshDot = (id) -> $.ajax url: "/dots/#{id}"

我的咖啡脚本文件如下所示:

updateDot = (iDraggingID, newX, newY) ->
  $.ajax "/dots/#{iDraggingID}",
    type:'PUT'
    dataType:'json'
    data: {dot: {position_x: "#{newX}", position_y: "#{newY}"}}

refreshDot = (id) ->
  $.ajax
    url: "/dots/#{id}"
    type: "GET"
    dataType: 'json'
    success: (data) ->
    moveDot data['position_x'], data['position_y'], "dot#{id}"

handleDragStop = (event, ui) ->
  newX = parseInt( ui.offset.left );
  newY = parseInt( ui.offset.top );
  sDraggingID = $(this).attr 'id'
  iDraggingID = sDraggingID.match(/\d+/)[0]
  updateDot iDraggingID, newX, newY
  refreshDot(iDraggingID)

moveDot = (x, y, id) ->
  document.getElementById(id).style.left = "#{x}px";
  document.getElementById(id).style.top = "#{y}px";

moveDots = (x) ->
  moveDot(i['position_x'], i['position_y'], "dot#{i['id']}") for i in x

jQuery -> # starts all of this
  moveDots gon.dots #basically returns JSON with all elements from database
  $('.dot').draggable
    containment: '#parent',
    cursor: 'move',
    stop: handleDragStop #when dragging an element is done trigger this
和我的控制器:

class DotsController < ApplicationController

def index
  @dots = Dot.all
  respond_to do |format|
    format.html { render :nothing => true }
    format.json { render :json => @dots, :status => :ok }
  end
end

def show
  @dot = Dot.find(params[:id])

  respond_to do |format|
    format.html  { render :nothing => true }
    format.json  { render :json => @dot, :status => :ok }
  end
end

def create
  @dot = Dot.new(dot_params)
  @dot.save
end

def update
  @dot = Dot.find(params[:id])
  @dot.update_attributes(dot_edit_params)

  respond_to do |format|
    format.html  { render :nothing => true }
    format.json  { render :json => @dot, :status => :ok}
  end
end

def destroy
   @dot = Dot.(params[:id])
   @dot.destroy
end
private
  def dot_params
    params.require(:dot).permit(:name, :position_x, :position_y)
  end

  def dot_edit_params
    params.require(:dot).permit(:position_x, :position_y)
  end
end
class DotsControllertrue}
format.json{render:json=>@dots,:status=>:ok}
结束
结束
def秀
@dot=dot.find(参数[:id])
回应待办事项|格式|
format.html{render:nothing=>true}
format.json{render:json=>@dot,:status=>:ok}
结束
结束
def创建
@点=点。新(点参数)
@点保存
结束
def更新
@dot=dot.find(参数[:id])
@点。更新属性(点编辑参数)
回应待办事项|格式|
format.html{render:nothing=>true}
format.json{render:json=>@dot,:status=>:ok}
结束
结束
def销毁
@点=点。(参数[:id])
@摧毁
结束
私有的
def dot_参数
参数要求(:点)。允许(:名称,:位置x,:位置y)
结束
def dot_edit_参数
参数要求(:点)。允许(:位置x,:位置y)
结束
结束
为什么有时候当我从CoffeeScript调用refreshDot时,我会得到位置x和位置y的正确值…有时候我会得到旧值???似乎是70:30好:坏回报


这是因为GET方法在PUT方法实际结束之前被调用了。。还是怎样我很困惑,使用异步(AJAX)请求的本质意味着第二个可能在第一个之前完成。如果存在依赖关系,请考虑使用回调。

像这样:

updateDot = (iDraggingID, newX, newY) ->
  $.ajax "/dots/#{iDraggingID}",
    type:'PUT'
    dataType:'json'
    data: {dot: {position_x: "#{newX}", position_y: "#{newY}"}}
    success: (data) ->
      moveDot data['position_x'], data['position_y'], "dot#{id}"

handleDragStop = (event, ui) ->
  newX = parseInt( ui.offset.left );
  newY = parseInt( ui.offset.top );
  sDraggingID = $(this).attr 'id'
  iDraggingID = sDraggingID.match(/\d+/)[0]
  updateDot iDraggingID, newX, newY

使用异步(AJAX)请求的本质意味着第二个请求可能在第一个请求之前完成。如果存在依赖关系,请考虑使用回调。

像这样:

updateDot = (iDraggingID, newX, newY) ->
  $.ajax "/dots/#{iDraggingID}",
    type:'PUT'
    dataType:'json'
    data: {dot: {position_x: "#{newX}", position_y: "#{newY}"}}
    success: (data) ->
      moveDot data['position_x'], data['position_y'], "dot#{id}"

handleDragStop = (event, ui) ->
  newX = parseInt( ui.offset.left );
  newY = parseInt( ui.offset.top );
  sDraggingID = $(this).attr 'id'
  iDraggingID = sDraggingID.match(/\d+/)[0]
  updateDot iDraggingID, newX, newY

我试着这样做smth(使用成功回调:)
updateDot=(iDraggingID,newX,newY)->$.ajax“/dots/{iDraggingID}”,类型:'PUT'数据类型:'json'数据:{dot:{position x:{newX},position y:{newY}}成功:refreshDot iDraggingID
,但没有区别。除了“成功”还有其他方法吗。。比如“当事情完成时:”回调?;>您还需要删除
handleDragStop
函数中的
refreshDot
调用。。我刚刚做了所有这些,而且更糟。。现在它总是返回坏结果。有没有什么方法可以用来延迟get,让它有更多的时间更新?如果没有refreshDot功能(我更新了我的示例代码),那就足够了。另外,请检查咖啡脚本中的缩进。moveDot调用需要在成功回调下缩进。可能是因为我在调用其他ajax请求的回调中调用ajax请求?;>>我试着这样做smth(使用成功回调:)
updateDot=(iDraggingID,newX,newY)->$.ajax“/dots/{iDraggingID}”,类型:'PUT'数据类型:'json'数据:{dot:{position x:{newX},position y:{newY}}成功:refreshDot iDraggingID
,但没有区别。除了“成功”还有其他方法吗。。比如“当事情完成时:”回调?;>您还需要删除
handleDragStop
函数中的
refreshDot
调用。。我刚刚做了所有这些,而且更糟。。现在它总是返回坏结果。有没有什么方法可以用来延迟get,让它有更多的时间更新?如果没有refreshDot功能(我更新了我的示例代码),那就足够了。另外,请检查咖啡脚本中的缩进。moveDot调用需要在成功回调下缩进。可能是因为我在调用其他ajax请求的回调中调用ajax请求?;>>