views:

314

answers:

5

How can i turn this:

<? echo json_encode($myArrays); ?>

...into this:

_rowData: [
    { name: "Most Recent", view: "recentView" }, 
    { name: "Most Popular", view: "popularView" }, 
    { name: "Staff Picks", view: "staffView" }
],

My script returns that ^, but i dont know how to put the data into the string, _rowData ? P.S. I am using Dashcode, trying to dynamically load items into a List Controller

So far, i have this:

var recentListControllerXHR = $.ajax("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data){
                            return(JSON.stringify(data));
                          }, 'text');

rowData: recentListControllerXHR,
+1  A: 

Try this:

var rowData;
$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data) {
    rowData = data;
});

But note that rowData is not available until the callback function (see second parameter of getJSON call) has been called.

Gumbo
I love how @Vincent Robert says that the question writer doesn't understand the $.ajax call and then follows that by demonstrating that he doesn't either. At least someone actually knows how to use it...
KyleFarris
A: 

Your function will return the data on a successful response. You have declared the callback function:

function(data){
                        return(JSON.stringify(data));
                      }

So, 'data' will contain any data that was returned from the request, if you're declaring your content type as text/json (or application/json - I don't remember off the top of my head) and rendering your JSON as text in the response you should be good.

What you probably want to do is have your function declare the variable as rowData and go from there, so do something like:

function(rowData){
           // do something with the rowdata
                          }

I doubt you need the stringify method unless you're trying to write the data out as text.

Lewis
+1  A: 

Ok - your problem appears to be a misunderstanding of how asynchronous APIs work. $.ajax() makes a request, and at some point in the future calls the provided callback with the response.

Assuming you have a name for the object you're populating, you can fill in the desired property using something like this:

var someObject = {
  ...
  rowData: null,
  ...
};

// at this point, someObject is incomplete...

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", 
  function(data)
  {
    // called whenever the server gets around to sending back the data
    someObject.rowData = data;
    // at this point, someObject is complete.
  });

Note that I'm using jQuery's built-in support for JSON here. You can use the json.org library instead if you wish, but getJSON() is rather convenient if you don't have any unusual needs when parsing the data.

Shog9
Yes, probably the best answer overall.
KyleFarris
ok, i modified this a bit - loading indicators and all.. but anyway this time when i alert `data` i get[Object object],[Object object],[Object object]Which i assume is now correct? I will test it out and let you know
tarnfeld
Well, that's been my point of confusion the whole time - if you're looking for an array of objects, this code is correct (that's what you're seeing printed out - use Firebug if you want all the details). If you actually wanted to preserve the JSON string, you'd use `$.ajax()` as in your original code - but I can't imagine why you'd want that. Since you've accepted this, I'll assume it worked for you and clarify your title to reflect my interpretation, but if not then let us know...
Shog9
Yeah works great!! I am having a little issue related to the DOM and reloading the list's data but thats nothing to do with this question, thank you so much! Do you have an email, i want to add you onto the credits for the app - haha
tarnfeld
A: 

Looks like you don't understand how $.ajax works (looks like nobody do).

$.ajax is an asynchronous function, which means it does not return anything. It only prepares a function to be invoked later, when the data has been received.

Your code should look more like that:

var obj = {
 ...
 _rowData: [],
 ...
};

$.getJSON("http://tarnfeldweb.com/applewebapps/ajax/recentApps.php", function(data)
{
    // Now data contains your row data, 

    // you can either fill the global object
    obj._rowData = data;
    doSomethingWith(obj);

    // or use data directly
    doSomethingWith({
        ...
        _rowData: data
        ...
    });  
});

// Note that code below will be executed before the AJAX data is received
Vincent Robert
Unfortunately, your code will not convert the response into a JSON object... it's just text.
KyleFarris
Edited into something that should work with a JSON response. Beware that there is no such thing as a JSON object in Javascript. You can create a Javascript object from a JSON string, and it looks like your _rowData is a Javascript object. This part of converting the JSON string into an object should be handled gracefully by jQuery.
Vincent Robert
+1  A: 

The question's essentially already been answered using another method, but, if you're interested in using the $.ajax method as opposed to the $.getJSON method, this is how you would do that:

var rowData;
$.ajax({
    url: "http://tarnfeldweb.com/applewebapps/ajax/recentApps.php",
    type: 'get',
    dataType: 'json' // could also be 'jsonp' if the call is going to another site...
    success: function(data){
        rowData = data; 
    }
});

Just another option, that's all...

KyleFarris