[ 2 posts ]

Wei Wang

YUI Contributor

  • Username: Shinwei
  • Joined: Wed Oct 27, 2010 1:45 pm
  • Posts: 91
  • GitHub: onlywei
  • Gists: onlywei
  • Offline
  • Profile

Autocomplete with DataSource.IO wrapped response object

Post Posted: Wed Feb 22, 2012 12:28 pm
+0-
I'm really confusing myself here reading the Autocomplete documentation.

I'm using a DataSource.IO instance to use as my Autocomplete's source, and I'm trying to configure the ResultsListLocator. From the examples, the response objects seem like pure JSON data, but my response object is an array of XMLHttpRequest objects.

I know that my actaul response JSON should look like:

Quote:
{ "items": ['one','two','three'], "numRows": 1 }


Therefore I WANT to just set my ResultListLocator to "items".

Unfortunately, that does not work, and upon inspecting what the response object actually looks like, it looks like the FireBug screenshot below:

Image

Do I really have to type a big resultListLocator function to penetrate this response object?

I tried plugging the datasource-jsonschema into the datasource instance, but that caused my resultListLocator function to never even get called.

Any ideas?

Ryan Grove

YUI Developer

  • Username: rgrove
  • Joined: Tue Dec 09, 2008 9:55 pm
  • Posts: 275
  • Location: Portland, OR
  • Twitter: yaypie
  • GitHub: rgrove
  • Gists: rgrove
  • IRC: rgrove
  • YUI Developer
  • Offline
  • Profile

Re: Autocomplete with DataSource.IO wrapped response object

Post Posted: Wed Feb 22, 2012 12:51 pm
+0-
When you use a DataSource as an AutoComplete source, it's generally best to process the response into a usable format using a DataSchema of some kind so that the object that ultimately gets passed from the DataSource to AutoComplete is something AutoComplete can handle (or at least close to it). Mixing a DataSource with AutoComplete's own result locating functionality can make it hard to understand which part of a response is being processed in which place. DataSchemas do take some configuration though (they don't just magically know what to do with your data).

In this case, it looks like the DataSource is returning an array with an unparsed XMLHttpRequest object as the first item. You would need to extract that item's `responseText`, parse it as JSON to get an object, and *then* AutoComplete would be able to operate on that object.

If you're not taking advantage of the DataSource to provide data to multiple components or to perform some kind of complex schema processing, then I'd recommend getting rid of it and just letting AutoComplete handle the XHR requests itself. It will then automatically parse the responses into objects, and you can set your resultListLocator to the string "response.items" (no function needed).
  [ 2 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum