Topic: jmaki_widget 'yahoo.dataTable' subscribe question

I am new to Rails development, and especially new to jmaki. I am modifying existing code, and would like to update the content of a yahoo.dataTable widget based on a set of radio buttons. It is difficult to find a template for this. My widget definition is:

<%=  jmaki_widget 'yahoo.dataTable', :subscribe=> "/jmaki/table",
:value => {:columns => [
     { :label => 'PRIORITY', :id => 'rule_priority'},
     { :label => 'GERMPLASM TYPE', :id => 'germplasm_type'},
     ....
     { :actions => 'Actions', :id => 'Actions'}
     ],
:rows => @rows_data
  }

and my button definition (on the same page) is:

<%= radio_button :allocation_rule, :rule_type, "INC" %>

My question is: how do I create the required publish/subscribe javascript to allow the widget to update the contents of the table with new @rows_data?

Any help would be greatly appreciated!

Last edited by NormalGuy (2012-01-27 13:08:34)

Re: jmaki_widget 'yahoo.dataTable' subscribe question

In case the answer may help some future newbie, here is what I did.

I created a field observer on the radio button:

  <%= observe_field 'allocation_rule_rule_type_inc',
    :on => 'click', :function => "var ruleType = 'INC';
    var referenceId = document.getElementById('allocation_rule_incoming_order_id').value;
    var baseUrl = '" + allocation_rules_url + "';
    jmakiPublishTable(baseUrl, ruleType, referenceId);"
  %>

My jmakiPublishTable function sends an Ajax request to my controller:

function jmakiPublishTable(baseUrl, ruleType, referenceId) {
  jmaki.publish('/jmaki/table/clear', {} );
  var url = baseUrl + '/selected_rule_type?rule_type=' + ruleType + '&reference_id=' + referenceId;
  new Ajax.Request(url, {
    method: 'get',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    onSuccess: function(transport){
      var rows2 = transport.responseText.evalJSON();
      jmaki.publish('/jmaki/table/addRows', {value : rows2 });
    }
  });
}

In the controller I set the variable @allocation_rules_list and respond_to format.json. I then wrote the view file selected_rule_type.json.erb to render json format:

<%= "[" %>
<% @allocation_rules_list.each_with_index do |ar, index| %>
  <% if index > 0 %>
    <%= "," %>
  <% end %>
  <%= "{ \"rule_priority\" : \"#{ar.rule_priority.to_s}\",
  \"rule_type\" : \"#{ar.rule_type.to_s}\",
  \"reference_id\" : \"#{ar.reference_id.to_s}\",
  \"effective_date\" : \"#{ar.effective_date.to_s}\",
  ....
<% end %>
<%= "]" %>

I did not use the to_json method of @allocation_rules_list because it also rendered the object type in the json output.

This is a bit cumbersome because I need observers on each radio button separately. I also see the issue of a radio button only registering the first click, so I may need to change to a observe_form or some other way to trigger the asynchronous update.

There may be a better way to do this; if so I would love to hear it! I any case, this is working for me.