Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] YAHOO.util.LocalDataSource with a JSON object?

Expand Messages
  • Satyam
    If that data is local, it is not really JSON but an actual array. JSON is a JavaScript object encoded into a string. This is a real array. var myData = [
    Message 1 of 2 , Aug 6, 2009
    • 0 Attachment
      If that data is local, it is not really JSON but an actual array. JSON
      is a JavaScript object encoded into a string. This is a real array.

      var myData = [
      {"id":"43846","name":"21st Century Home Mortgage Corporation"},
      {"id":"43847","name":"507 Prudential Road"},
      {"id":"43848","name":"5th & 3rd Bank"}
      ];


      myDataSource = new YAHOO.util.LocalDataSource(myData);
      myDataSource.responseSchema = {
      fields : ["name","id"]
      };

      You don't need to specify the type, it will realize it is an array on
      its own. There is no resultsList either. Quotes around the field names
      in the array are not needed, but they can remain there.

      Also, avoid trailing commas such as that in the third record, that will
      cause trouble. Probably the data you are showing is just a segment and
      the comma is not there in the actual last record. This is not a YUI
      problem but of the JavaScript interpreter.

      Satyam

      electricspeed escribió:
      > I'm trying pass myData, which is a JSON object, to YAHOO.util.LocalDataSource, and then use the resulting data source with AutoComplete -- but I can't get it to work.
      >
      > I am new to YUI and have autocomplete working using XHRDataSource(url), where the URL queries the DB, but I want to speed up the response time by eliminating the DB queries so I'm including the entire dataset in the Web page and trying to LocalDataSource instead.
      >
      > I cannot get it to work with a JSON object -- where am I going wrong?
      >
      > var myData = [
      > {"Company":{"id":"43846","name":"21st Century Home Mortgage Corporation"}},
      > {"Company":{"id":"43847","name":"507 Prudential Road"}},
      > {"Company":{"id":"43848","name":"5th & 3rd Bank"}},
      > ];
      >
      >
      >
      > (function() {
      >
      > myDataSource = new YAHOO.util.LocalDataSource(myData);
      > //myDataSource = new YAHOO.util.XHRDataSource(url);
      > myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
      > myDataSource.responseSchema = {
      > resultsList : "Company",
      > fields : ["name","id"]
      > };
      >
      > var myAutoComp = new YAHOO.widget.AutoComplete("myInput","myContainer", myDataSource);
      > myAutoComp.applyLocalFilter = true;
      >
      > return {
      > oDS: myDataSource,
      > oAC: myAutoComp
      > };
      >
      > })();
      >
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      > ------------------------------------------------------------------------
      >
      >
      > No virus found in this incoming message.
      > Checked by AVG - www.avg.com
      > Version: 8.5.392 / Virus Database: 270.13.45/2285 - Release Date: 08/06/09 05:57:00
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.