Topic: Rails ActiveRecord-JSON to Scal Planner

Hi,

I was able to convert an ActiveRecord object into JSON with the following code:

#events_controller.rb
def summary
  @event = Event.find(:all)
end

#summary.html.erb
<%= hidden_field_tag 'json_event', @events.to_json %>

To make this Rails string into JavaScript code and place each event into the calendar planner:

#still in summary.html.erb
<script>
myplanner = generatePlanner($('json_event').value);
samplecal = new scal('samplecal', checkPlannerElement, {planner: myplanner});
</script>

#public/javascripts/util/application.js
function generatePlanner(p_events) {
  var events = eval(p_events);
  var month_str = [
    '','January','February','March','April','May','June','July',   
    'August','September','October','November','December'
  ];
 
  var myplanner = [];

  if (events.length > 0) {
    for (var i = 0; i < events.length; i++) {   
      due_at = events[i].due_at.split(" "); //events[i]["due_at"].split(" ");                   
      due_date = due_at[0].split("/");
      month_index = parseInt(due_date[1]) < 10 ? parseInt(due_date[1].replace("0","")) :
      parseInt(due_date[1]);
           
      day_num = parseInt(due_date[2]) < 10 ? due_date[2].replace("0","") : due_date[2];
      long_date  = month_str[month_index] + " " + day_num + "," + due_date[0];               
      obj_event = { period: long_date, label: events[i].description, cls: 'reminders'
      myplanner.push(obj_event);
    }
  }
  return myplanner;
}


Just my utility to check object's properties when an event in the planner is click
<script>
function checkPlannerElement(d) {
  var evts = samplecal.getElementByDate(d);
  var obj_property = "";
  var col = 0;
 
  for (property in evts) {
    col += 1
    if( col < 11) {
      obj_property += property + ", ";
    } else {
      obj_property += property + "\n";
      col = 0;
    }
  }
                           
  alert(obj_property);                           
}
</script>

I guess everything were already in place but I am getting stuck in getting this functionality to work, when a link in the Calendar (Scal Planner)(http://scal.fieldguidetoprogrammers.com … l-planner/) is click, this should redirect to a Rails controller for edit (carrying along the events id).

Any help from JS Gurus will be much appreciated. Thanks.

Last edited by joboy (2008-12-05 19:03:42)