Search This Blog

Saturday, July 19, 2014

AJAX calls from core_ajax_dart

The first phase is 
  1. Make a call to the YouTube api (a hardcoded url and parameters).  
  2. Parse and display the response.
Problems I encountered.
I would like to bind the "response" attribute from the core_ajax_dart element to a object in my custom element (api-wrapper).  When triggered by the core-response event, a function in the api-wrapper would process the object.  I kept getting "null" in object (the default response value).  I don't know how long the response persists.  
I solved the problem by capture the detail of the core-response event in a function in the api-wrapper element.

handleResponse(Event e, var detail, Node sender){
print(detail['response']);
parseResponse(detail['response']);
}

Parsing the response

I would like to use JSON.decode to parse the response (detail).  I convert to a string and then pass to JSON.decode.  I get a FormatException.  
After searching, I found that the response is a collection.  I can use [selectors] to navigate to the fields I want.  For example:

handleResponse(Event e, var detail, Node sender){
e.preventDefault();
apiResponse = detail['response'];
print(apiResponse['feed']['entry'].length);
print(apiResponse['feed']['entry'][0]['title'][r'$t']);
}

This gets me close.  Ideally I would iterate through the list in the DOM to display the desired fields.  I haven't got this to work yet.

I can use a for-in loop in my dart file to display the fields from the response.  Now, I need to get the values onto the page.

No comments:

Post a Comment