如何重构用 jQuery/Coffeescript 编写的功能代码以实现可重用性
以下代码对我有用:
$ ->
$("[id$=\"_phase_id\"]").change(->
common_prefix = "type_well_type_well_phases_attributes_"
cell_pos = $(this).attr("id").match(/\d+/g)
mult = "#" + common_prefix + cell_pos + "_multiplier"
mol = $("#" + common_prefix + cell_pos + "_mol_percent").val()
gpm = "#" + common_prefix + cell_pos + "_GPM"
component_id = $(this).val()
$.getJSON('/phases/' + component_id, (data) ->
pressure_base = 0.0
if $("#type_well_pressure_base").val() == "14.65"
pressure_base = data.base1
else if $("#type_well_pressure_base").val() == "14.696"
pressure_base = data.base2
else if $("#type_well_pressure_base").val() == "14.73"
pressure_base = data.base3
else if $("#type_well_pressure_base").val() == "15.025"
pressure_base = data.base4
mult_value = parseFloat(pressure_base)
$(mult).val mult_value
$(gpm).val bcr_round_to(mult_value * (mol / 100.0), 4) if (mol != "")
)
).trigger "change"
这是遍历我的 Rails 代码中的嵌套表单 (phase_attributes),以根据我的父表单 (type_well) 中选择的值 (presure_base 下拉列表) 调用 json,然后计算一个值 (gpm)对于嵌套形式。正如您所看到的,如果阶段下拉列表中的阶段“更改”,我会从数据库中获取相应的行,并根据 pressure_base 的值(14.65、14.696、14.73、15.025)计算“gpm”值。一切正常。
现在,我试图将其功能设置为当用户更改“Pressure_base”本身时自动重新计算嵌套表单的“gpm”值。显然,我必须循环遍历嵌套行(我知道如何做到这一点,所以这没有问题),并且基本上对每个嵌套行再次重复该过程。这里唯一的变量是“cell_pos”。有什么方法可以提取从循环中重复调用的从属 jQuery/Coffeescript 函数吗?我知道如何在 Ruby/Rails 中做到这一点,因为我对它很满意,但对 jQuery 的经验较少(对于 Coffeescript 的经验更少)。
The following code works for me:
$ ->
$("[id$=\"_phase_id\"]").change(->
common_prefix = "type_well_type_well_phases_attributes_"
cell_pos = $(this).attr("id").match(/\d+/g)
mult = "#" + common_prefix + cell_pos + "_multiplier"
mol = $("#" + common_prefix + cell_pos + "_mol_percent").val()
gpm = "#" + common_prefix + cell_pos + "_GPM"
component_id = $(this).val()
$.getJSON('/phases/' + component_id, (data) ->
pressure_base = 0.0
if $("#type_well_pressure_base").val() == "14.65"
pressure_base = data.base1
else if $("#type_well_pressure_base").val() == "14.696"
pressure_base = data.base2
else if $("#type_well_pressure_base").val() == "14.73"
pressure_base = data.base3
else if $("#type_well_pressure_base").val() == "15.025"
pressure_base = data.base4
mult_value = parseFloat(pressure_base)
$(mult).val mult_value
$(gpm).val bcr_round_to(mult_value * (mol / 100.0), 4) if (mol != "")
)
).trigger "change"
This is walking through a nested form (phase_attributes) in my Rails code to call json based on a value (presure_base drop-down) selected in my parent form (type_well) and then compute a value (gpm) for the nested form. As you can see, if the phase is "changed" in the phases drop-down, I fetch the corresponding row from database and based on the value of pressure_base (14.65, 14.696, 14.73, 15.025), I compute the "gpm" value. It is all working.
Now I am trying to put it the functionality to automatically recompute the nested form's "gpm" values when the "pressure_base" itself is changed by the user. Clearly, I have to loop through the nested rows (I know how to do it so that is no problem), and basically repeat the process again for each nested row. The only variable here is the "cell_pos". Is there any way that I can extract out a subordinate jQuery/Coffeescript function which is called from my loop repeatedly? I know how to do it in Ruby/Rails since I am quite comfortable with it, but have less experience with jQuery (even less with Coffeescript).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您应该使用
#type_well_Pressure_base
上的change
事件来重新计算gpm
值。然后,您可以使用 getJSON 回调来更改存储的值并触发计算。If I understood you correctly, you should use the
change
event on#type_well_pressure_base
to recalculategpm
value. Then you can use the getJSON callback just to change the stored values and trigger the computation.