简体   繁体   中英

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. Using print statements in the js.coffee file I can see the controller has returned the correct data. Within the js.coffee file I have unsuccessfully tried a number of different attempts to access this data to put on a screen partial. 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

assets/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

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…}

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

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

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).

thanks to all those who checked this question

Pierre

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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