簡體   English   中英

如何在javascript回調中訪問更新的對象(而不是舊對象)

[英]How to access the updated object in a javascript callback (instead of the old object)

這里介紹的有用且有效的解決方案的基礎上,我也試圖修復更新回調。

問題是,即使此回調是由成功的update操作觸發的,我嘗試從中提取數據的特定unit始終是舊的緩存版本。

// callback triggered by the update action

$('.best_in_place').bind("ajax:success", function () {
   ...
    console.log(unit.duration);
    // which is exactly the same as
    console.log(<%= Unit.find(unit.id).unit_users.pluck(:duration).sum %>);
    // and both print the OLD duration val instead of the updated val which is in the database
});

和unit_users_controller代碼...

  def update
    @unit = @unituser.unit

    respond_to do |format|
      if @unituser.update(unit_user_params)
        @unit.reload
        logger.info('-----------------------------------------------------------------')
        logger.info('@unit.duration in the controller is ' + @unit.duration.to_s) # which is the correct value
        logger.info('-----------------------------------------------------------------')
        gon.unit_duration = @unit.duration # an experiment which didn't work for me
        format.json {respond_with_bip(@unituser) }
      else
        # format.html { render :action => 'edit' }
        format.json { respond_with_bip(@unituser) }
      end
    end
  end

我已經嘗試了幾個版本的unit.reload ,但沒有任何幫助。 也許我把它放錯了地方?

這與緩存無關。 在將JavaScript發送到客戶端之前,您的Ruby代碼在服務器端進行了評估,並且僅在AJAX請求發生之前就進行了一次評估。

客戶永遠不會看到這一行:

console.log(<%= Unit.find(unit.id).unit_users.pluck(:duration).sum %>);

客戶將看到的所有內容如下:

console.log(32); // or whatever the sum is

您不能在此處使用<%= %> 這將始終為您提供原始價值。 相反,您需要將新值發送到客戶端以響應AJAX請求。

我前段時間做過的這是我的代碼,也許它將對您有所幫助:

Javascript:

$(document).ready(function() {
  $('.price_bind').bind("ajax:success", function (event, data, status, xhr) {
      var parsed_data = jQuery.parseJSON(data);
      $(this).text(parsed_data.newprice);
      $(this).parentsUntil('body').find(".totalpricep span").text(parsed_data.totalprice);
  });
}

視圖:

<%= best_in_place detail, :price, :classes => 'price_bind', :path => purchase_detail_path(@purchase, detail)%>

控制器:

 def update
    respond_to do |format|
      if @detail.update_attributes(params[:detail])
        @n=@detail.mk_bal
        @r=false
        if @detail.purchase != nil
          @p=@detail.purchase.totalprice
        if params[:detail]['status'] && @purchase.step==1
          @remdet = @purchase.details.where(:step => 1, :status => false)
          if @remdet.empty?
            @purchase.update_attribute(:step, 2)
            @r=true
          end  
        end
        else
          @p=nil
        end
        format.html { redirect_to @detail, notice: 'Detail was successfully updated.' }
        format.json { render :json => {:newprice => @n, :totalprice => @p, :newstatus => @detail.status, :refresh => @r}}
      else
        format.html { render action: "edit" }
        format.json { render json: @detail.errors, status: :unprocessable_entity }
      end
    end
  end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM