简体   繁体   English

在javascript / js.coffee中访问json数据

[英]Accessing json data within javascript/js.coffee

In a js.coffee file in assets/javascript I am unsuccessfully attempting to respond to data I received from a call to a controller. 在资产/ javascript中的js.coffee文件中,我尝试响应从控制器调用中收到的数据,但未成功。 Using print statements in the js.coffee file I can see the controller has returned the correct data. 使用js.coffee文件中的打印语句,我可以看到控制器已返回正确的数据。 Within the js.coffee file I have unsuccessfully tried a number of different attempts to access this data to put on a screen partial. 在js.coffee文件中,我尝试了多种不同的尝试来访问此数据以将其放在屏幕的一部分上,但均未成功。 Below I have included the (1) js,coffee file as well (2) the relevant controller action (3) print statements from terminal console generated from the javascript/drills.js.coffee (4) the screen partial I am trying to populate within the javascript/drills.js.coffee 下面,我包括了(1)js,coffee文件(2)相关的控制器操作(3)从终端控制台打印的语句,这些语句是从javascript / drills.js.coffee生成的(4)我试图填充的屏幕部分在javascript / drills.js.coffee中

assets/javascript/drills.js.coffee 资产/javascript/drills.js.coffee

changeAssump = (data)-> 
   console.log("-6-  in changeAssump  ")
    console.log("-6- print data from call")
    console.log data
    # this is what I can't do
    # trying to get values Drills controller action user_valuation_selected to be displayed
    # on the screen partial assumption_params   
    # e.g ('#evaluation_assumption_resource_estimation_gas').val(resource_estimation_gas)
    # where resource_estimation_gas is coming from the returned object

# this ok (I think) and calling above
changeAssumptions = (drill_id)->
   data = $('#user_save_name_evaluation_assumption_id').serialize()
   console.log("-4- in changeAssumptions")
   console.log data
   $.ajax
    url:"/drills/#{drill_id}/user_valuation_selected.json"
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    success: (data,success,xhr)->
        changeAssump data

drills.controller.rb drills.controller.rb

def user_valuation_selected
        @userassum = EvaluationAssumption.find(params[:user_save_name][:evaluation_assumption_id])
          respond_to do |format|
        format.json { render :json => @userassum }
    end
end

Developer tools -> console -> output from print statements above 开发人员工具->控制台->上面打印语句的输出

-> Object {created_at: "2013-09-26T00:36:34Z", drill_id: 1, id: 51, in_ground_value_gas: 1, in_ground_value_oil: 5…} ->对象{created_at:“ 2013-09-26T00:36:34Z”,drill_id:1,id:51,in_ground_value_gas:1,in_ground_value_oil:5…}

when expand by clicking on arrow (->) this is the data I cannot access in the javascript/drills.js.coffee eg I want accesss to resource_estimation_gas etc 当通过单击箭头(->)展开时,这是我无法在javascript / drills.js.coffee中访问的数据,例如,我希望访问resource_estimation_gas等

assets/javascript/drills.js.coffee created_at: "2013-09-26T00:36:34Z" drill_id: 1 id: 51 in_ground_value_gas: 1 in_ground_value_oil: 5 member_profile_id: 1 probability_of_success: 11 resource_estimation_gas: 1 resource_estimation_oil: 1.168 asset / javascript / drills.js.coffee created_at:“ 2013-09-26T00:36:34Z” drill_id:1 id:51 in_ground_value_gas:1 in_ground_value_oil:5 member_profile_id:1可能性_成功:11 resource_estimation_gas:1 resource_estimation_oil:1.168

screen partial 屏幕局部

# as included within main screen
<div class="form assumption" data-drillid="<%= @drill.id %>">
    <div id="id_assumption_params">
            <%= render 'assumption_params' %>
    <div>
    </div>


# part of the partial assumption_params.html.erb

<%= simple_form_for @evaluation_assumption,
    :html => { class: 'infogroup', 
    id: "evaluation_assumption_params"  } do |f| %>
…
…
<%= content_tag :td, f.text_field(:resource_estimation_gas, 
              class: "general ralign"), class: 'data' %>
  <%= content_tag :td, f.text_field(:resource_estimation_oil, 
              class: "general ralign"), class: 'data' %>

prefixing the value I want with data gets the derisred result. 给我想要的值加上数据前缀会得到令人讨厌的结果。

For example to get the value for the saved evaluation_assumption resource_estimation_gas, I reference val(data.resource_estimation_gas). 例如,要获取已保存的valuation_assumption resource_estimation_gas的值,我引用val(data.resource_estimation_gas)。

thanks to all those who checked this question 感谢所有检查此问题的人

Pierre 皮埃尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM