Hunter
Hunter

Reputation: 1

Rendering event data with fullcalendar in Zend Framework

I am incorporating jquery's fullcalendar into my ZF application. I can get the calendar to show up in my view, but as soon as I add a JSON URL to the events parameter of fullcalendar and try to send it my event data, all that happens in the JSON string is output to the browser.

Here's the relevant code in my controller:

$sfaCalendar = new Model_Exchange();
$sfaCalendar->getCalendarEntries($startDateTime,$endDateTime,'Calendar');
$this->_helper->json($sfaCalendar->data);

Here's my view script:

<?php
$this->headLink()->appendStylesheet('/styles/module/urpower/sfa/fullcalendar.css');
$this->headLink()->appendStylesheet('/styles/module/urpower/sfa/jquery-ui-1.8.16.custom.css');
$this->headScript()->prependScript(
"   $(document).ready(function() {
        $('#calendar').fullCalendar({ 
            header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        events: '/urpower/sfa/calendar',        
        theme: true,
        });
    });
");

$this->headScript()->appendFile('/scripts/fullcalendar/fullcalendar.min.js');

?>
<div id='calendar'></div>

The URL in my events parameter is the path to the controller where I create the JSON encoded data.

I've checked the validity of my JSON string and it looks fine. Any ideas why fullcalendar isn't grabbing my data and putting it into the calendar?

Here's my JSON:

[
   {
      "title":"Add notes to Innotas notes",
      "start":"2012-01-02T16:30:00Z"
   },
   {
      "title":"Test appointment #1",
      "start":"2012-01-03T21:00:00Z"
   },
   {
      "title":"Test appointment #2",
      "start":"2012-01-03T22:00:00Z"
   },
   {
      "title":"Test appointment #3",
      "start":"2012-01-03T22:30:00Z"
   },
   {
      "title":"Add notes to Innotas notes",
      "start":"2012-01-09T16:30:00Z"
   },
   {
      "title":"Add notes to Innotas notes",
      "start":"2012-01-16T16:30:00Z"
   },
   {
      "title":"SFA Phase 2 - status, testing, etc.",
      "start":"2012-01-19T18:30:00Z"
   },
   {
      "title":"Add notes to Innotas notes",
      "start":"2012-01-23T16:30:00Z"
   },
   {
      "title":"RLK Integration",
      "start":"2012-01-23T17:00:00Z"
   },
   {
      "title":"FW: Quarterly Employee Gathering",
      "start":"2012-01-27T17:30:00Z"
   },
   {
      "title":"Meeting with KAREN DUFFY X423  ALT # DI\/PUMPKIN RIDGE GOLF CLUB",
      "start":"2012-01-30T14:15:00Z"
   },
   {
      "title":"Add notes to Innotas notes",
      "start":"2012-01-30T16:30:00Z"
   }
]

Thanks!

Upvotes: 0

Views: 1184

Answers (2)

Hunter
Hunter

Reputation: 1

Okay, I finally got this to work (with a coworker's help, as well as digesting some of the ideas here.)

My calendar controller action is not where I should be using the JSON helper. This action just sets the view, where my fullcalendar JS resides. I created another action in the same controller, named calendarevents, and moved the code to retrieve the event data from my model here. Then I execute JSON helper command which sends the formatted data to the browser.

Then, in my calendar view, I changed the event source URL to point to the new calendarevents action, and badda bing, my event data is showing up in my calendar!

Seems to make a lot of sense now, but you know what they say about hind sight!

Upvotes: 0

RockyFord
RockyFord

Reputation: 8519

I found your problem, you are using the action helper version of the json helper which disables the viewrenderer.

24.8.4.5. JSON

The JSON action helper does several things:

Disables layouts if currently enabled.

Optionally, an array of options to pass as the second argument to Zend_Json::encode(). This array of options allows enabling layouts and encoding using Zend_Json_Expr.

$this->_helper->json($data, array('enableJsonExprFinder' => true));

Disables the ViewRenderer if currently enabled.

Sets the 'Content-Type' response header to 'application/json'.

By default, immediately returns the response, without waiting for the action to finish execution.

If you execute
$this->_helper->json($data, true); (where true = keep layouts)
you'll get the viewRenderer back, as enabling keeplayouts => true calls the view helper json:

//excerpt from:Zend_Controller_Action_Helper_Json
public function encodeJson($data, $keepLayouts = false)
    {
        /**
         * @see Zend_View_Helper_Json
         */
        require_once 'Zend/View/Helper/Json.php';
        $jsonHelper = new Zend_View_Helper_Json();
        $data = $jsonHelper->json($data, $keepLayouts);

        if (!$keepLayouts) {
            /**
             * @see Zend_Controller_Action_HelperBroker
             */
            require_once 'Zend/Controller/Action/HelperBroker.php';
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
        }

        return $data;
    }

Upvotes: 1

Related Questions