2

I'm trying to make jQuery Autocomplete, Yahoo Finance and Zend Framework work together.

What I want is to create a form field in which I can autocomplete tickers symbols through Yahoo API.

I've already created a Zend_From element that contains this :

    $this->setJQueryParam('source', new Zend_Json_Expr('function( request, response ) {
            $.ajax({
                type: "GET",
                dataType: "jsonp",
                jsonp: "callback",
                jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
                data: {
                    query: request.term
                },
                cache: true,
                url: "http://autoc.finance.yahoo.com/autoc";
                }
            });
        }'));
    $this->getView()->jQuery()->addJavascript('var YAHOO={Finance:{SymbolSuggest:{}}};');
    $this->getView()->jQuery()->addOnLoad('YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
                                            console.log(JSON.stringify(data)); }');

I found this post that resolved a part of my problem, but I think using var YAHOO={Finance:{SymbolSuggest:{}}}; is a dirty trick and isn't the right way to do.

Now, if I type GOO in my field, then the firebug console will show me something like this:

{"ResultSet":{"Query":"goo","Result":[{"symbol":"GOOG","name":"Google Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GT","name":"Goodyear Tire & Rubber Co.","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"}...

which is great, but I don't know how to send back these data to Autocomplete from this callback function, any idea?

Community
  • 1
  • 1
Liyali
  • 5,643
  • 2
  • 26
  • 40
  • Here is a good way to quickly try the yahoo finance lookup api: http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=GOO&callback=YAHOO.Finance.SymbolSuggest.ssCallback – Liyali Feb 08 '12 at 10:57
  • I'll add a caution that this autocomplete functionality provided by autoc.finance.yahoo.com is not a real API - it's an internal Yahoo function for their websites. You can certainly play with and build apps with it, but I wouldn't try to build anything real with it or you may run against their terms of service. – BrianC Feb 08 '12 at 23:13
  • Thanks for your comment, I'm actually developing a virtual portfolio manager. – Liyali Feb 09 '12 at 06:49
  • did you run into any issues due to the tos BrianC is mentioning? – HBCondo Mar 22 '12 at 05:55
  • None so far, I even ran a cron process to see how many requests Yahoo could handle from the same server, and it seems that one request every 8 sec (for 3 days) worked pretty good. – Liyali Mar 23 '12 at 09:45

2 Answers2

4

Liyali, thanks for figuring it out and posting the code. I just wanted to provide the js in the context of a jquery auto-complete:

$("#txtTicker").autocomplete({
source: function (request, response) {
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
        data: {
            query: request.term
        },
        cache: true,
        url: "http://autoc.finance.yahoo.com/autoc"
    });

    YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
        response($.map(data.ResultSet.Result, function (item) {
            return {
                label: item.name,
                value: item.symbol
            }
        }));
    }
},
minLength: 1,
select: function (event, ui) {
    $("#txtTicker").val(ui.item.name);
},
open: function () {
    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}

});

HBCondo
  • 895
  • 3
  • 10
  • 26
3

I have finally found a solution, but I'm still using YAHOO namespace though.

I first removed the last line (addOnLoad) and move its content right after the Ajax request so that I'm still into the autocomplete function and I can return the result to autocomplete.

Here is the final code:

$this->setJQueryParam('source', new Zend_Json_Expr('function( request, response ) {
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
            data: {
                query: request.term
            },
            cache: true,
            url: "http://autoc.finance.yahoo.com/autoc";
            }
        });
        YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
            console.log(data.ResultSet.Result);
            response( $.map( data.ResultSet.Result, function( item ) { 
                return {
                    label: item.symbol,
                    value: item.name
                }
            }))
         }
    }'));
$this->getView()->jQuery()->addJavascript('var YAHOO={Finance:{SymbolSuggest:{}}};');

Hope it can help!

Liyali
  • 5,643
  • 2
  • 26
  • 40