Skip to main content

jQuery JSON to Javascript



I am modifying a jQuery calendar and want to change this code:







function getEventData() {

var year = new Date().getFullYear();

var month = new Date().getMonth();

var day = new Date().getDate();



return {

events : [

{

"id":1,

"start": new Date(year, month, day, 12),

"end": new Date(year, month, day, 13, 30),

"title":"Lunch with Mike"

},

{

"id":2,

"start": new Date(year, month, day, 14),

"end": new Date(year, month, day, 14, 45),

"title":"Dev Meeting"

},

{

"id":3,

"start": new Date(year, month, day + 1, 17),

"end": new Date(year, month, day + 1, 17, 45),

"title":"Hair cut"

},

{

"id":4,

"start": new Date(year, month, day - 1, 8),

"end": new Date(year, month, day - 1, 9, 30),

"title":"Team breakfast"

},

{

"id":5,

"start": new Date(year, month, day + 1, 14),

"end": new Date(year, month, day + 1, 15),

"title":"Product showcase"

},

{

"id":6,

"start": new Date(year, month, day, 10),

"end": new Date(year, month, day, 11),

"title":"I'm read-only",

readOnly : true

},

{

"id":7,

"start": new Date(year, month, day + 2, 17),

"end": new Date(year, month, day + 3, 9),

"title":"Multiday"

}

]

};

}







I want to pull the JSON data from a database in a PHP page. So I it would have to look something like:







function getEventData() {



$.getJSON('../ajax/calendar.php?type=get', function(data) {



return data;

});



}







But I am getting an error saying "Uncaught TypeError: Cannot read property 'options' of undefined" which relates to the rendering of the data here:







_renderEvents: function(data, $weekDayColumns) {

var self = this;

var options = this.options;

var eventsToRender;



if (data.options) {

var updateLayout = false;

//update options

$.each(data.options, function(key, value) {

if (value !== options[key]) {

options[key] = value;

updateLayout = updateLayout || $.ui.weekCalendar.updateLayoutOptions[key];

}

});







The JSON returned:







[{"id":"1","start":"Wed Jan 25 2012 11:30:00 GMT-0800 (PST)","end":"Wed Jan 25 2012 11:45:00 GMT-0800 (PST)","title":"test"}]







Did a test on the PHP page and the JSON is displaying as it should. Any suggestions?


Comments

  1. I assume the data in _renderEvents: function(data, $weekDayColumns) { is the data you retrieve with your getEventData() method and store it there then. This being the case, you need to wait until the data actually has content that is returned from the ajax.

    The block of code _renderEvents: function(data, $weekDayColumns) { must not be executed before the ajax has finished. You could wrap it in a function and call it onDataDone(), and then in your getJSON callback call that function.

    var data = '';

    function getEventData() {
    $.getJSON('../ajax/calendar.php?type=get', function(result) {
    data = result;
    onDataDone();
    });
    }

    function onDataDone() {
    // code that wraps the _renderEvents: function(data, $weekDayColumns) {...
    }

    getEventData();

    ReplyDelete
  2. $.getJSON runs asynchronously so the return doesn't execute until later. You could either run an ajax request synchronously, or add a callback that's called when the ajax request has completed.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex