如何重构用 jQuery/Coffeescript 编写的功能代码以实现可重用性

发布于 2024-12-19 09:13:44 字数 1571 浏览 1 评论 0原文

以下代码对我有用:

  $ ->
    $("[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2024-12-26 09:13:44

如果我理解正确,您应该使用 #type_well_Pressure_base 上的 change 事件来重新计算 gpm 值。然后,您可以使用 getJSON 回调来更改存储的值并触发计算。

phase = $('[id$="_phase_id"]')
phase.change ->
    self = $(this)
    common_prefix = "type_well_type_well_phases_attributes_"

    cell_pos = self.attr('id').match(/\d+/g)
    component_id = self.val()

    cell = "##{common_prefix}#{cell_pos}"
    mult = "#{cell}_multiplier"
    gpm  = "#{cell}_GPM"

    mol  = $("#{cell}_mol_percent").val()

    $.getJSON("/phases/#{component_id}", (data) ->
        pressure.base.data 'pb_data', data
        pressure_base.trigger 'change'

base_values =
    '14.65'  : 'base1'
    '14.696' : 'base2'
    '14.73'  : 'base3'
    '15.025' : 'base4'

pressure_base = $('#type_well_pressure_base')
pressure_base.change ->
    data = $(this).data 'pb_data'
    type_well_pb = pressure_base.val()
    base = base_values[type_well_pb]
    mult_value = parseFloat(data[base])
    $(mult).val mult_value
    $(gpm).val bcr_round_to(mult_value * (mol / 100.0), 4) if (mol != "")

phase.trigger 'change'

If I understood you correctly, you should use the change event on #type_well_pressure_base to recalculate gpm value. Then you can use the getJSON callback just to change the stored values and trigger the computation.

phase = $('[id$="_phase_id"]')
phase.change ->
    self = $(this)
    common_prefix = "type_well_type_well_phases_attributes_"

    cell_pos = self.attr('id').match(/\d+/g)
    component_id = self.val()

    cell = "##{common_prefix}#{cell_pos}"
    mult = "#{cell}_multiplier"
    gpm  = "#{cell}_GPM"

    mol  = $("#{cell}_mol_percent").val()

    $.getJSON("/phases/#{component_id}", (data) ->
        pressure.base.data 'pb_data', data
        pressure_base.trigger 'change'

base_values =
    '14.65'  : 'base1'
    '14.696' : 'base2'
    '14.73'  : 'base3'
    '15.025' : 'base4'

pressure_base = $('#type_well_pressure_base')
pressure_base.change ->
    data = $(this).data 'pb_data'
    type_well_pb = pressure_base.val()
    base = base_values[type_well_pb]
    mult_value = parseFloat(data[base])
    $(mult).val mult_value
    $(gpm).val bcr_round_to(mult_value * (mol / 100.0), 4) if (mol != "")

phase.trigger 'change'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文