[英]How to access the updated object in a javascript callback (instead of the old object)
Building on the helpful and working solution presented here , I'm trying to fix my update callback as well. 在这里介绍的有用且有效的解决方案的基础上,我也试图修复更新回调。
Problem is, the specific unit
that I'm trying to extract data from is always the old cached version, even though this callback is triggered by a successful update
action. 问题是,即使此回调是由成功的
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
});
and the unit_users_controller code... 和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
I've tried several versions of unit.reload
, and nothing helps. 我已经尝试了几个版本的
unit.reload
,但没有任何帮助。 Maybe I was putting it in the wrong place? 也许我把它放错了地方?
This isn't about caching. 这与缓存无关。 Your Ruby code is evaluated server -side, before the JavaScript is ever send to the client, and it's only evaluated once, long before the AJAX request can happen.
在将JavaScript发送到客户端之前,您的Ruby代码在服务器端进行了评估,并且仅在AJAX请求发生之前就进行了一次评估。
The client never sees this line: 客户永远不会看到这一行:
console.log(<%= Unit.find(unit.id).unit_users.pluck(:duration).sum %>);
All the client will see is something like: 客户将看到的所有内容如下:
console.log(32); // or whatever the sum is
You cannot use <%= %>
here. 您不能在此处使用
<%= %>
。 That will always give you the original value. 这将始终为您提供原始价值。 Instead, you need to send the new value to the client in response to the AJAX request.
相反,您需要将新值发送到客户端以响应AJAX请求。
I did this one sometime ago here is my code, maybe it will help you: 我前段时间做过的这是我的代码,也许它将对您有所帮助:
Javascript: 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);
});
}
View: 视图:
<%= best_in_place detail, :price, :classes => 'price_bind', :path => purchase_detail_path(@purchase, detail)%>
Controller: 控制器:
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.