duckduckgo / zeroclickinfo-spice

DuckDuckGo Instant Answers based on JavaScript (JSON) APIs
https://duckduckhack.com/
Other
548 stars 942 forks source link

Javascript callback doesn't fire on DOMReady from Spice plugin #171

Closed bradcater closed 11 years ago

bradcater commented 11 years ago

I want to add a click handler to a link in a Spice template, and I expected the following javascript to add a click handler that would show some hidden elements when a link is clicked:

YAHOO.util.Event.onDOMReady(function() {
  YAHOO.util.Dom.get('spice_betterific_more').on('click', function(e) {
    YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getElementsByClassName('obj-wrapper'), 'display', 'block');
  });
});

I've verified that there is an element with the id spice_betterific_more on the page:

YAHOO.util.Dom.get('spice_betterific_more')

from the console selects the desired link.

Running

YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getElementsByClassName('obj-wrapper'), 'display', 'block');

shows the hidden elements, which is what I want to happen when the link is clicked. Is there a different way to hook the DOMReady event that I should use?

moollaza commented 11 years ago

@bradcater thanks for pointing this out. To be honest I'm not entirely sure why this isn't working, I can look into it if you'd like, but I'm glad to announce that we recently launched Spice v2, which you can read about here which, among other things, now includes jQuery.

I'm not sure if you're familiar with jQuery, but their documentation is excellent (much better than YUI2 docs in my opinion). Here is an example usage of a click handle in another spice plugin: https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/share/spice/github_jobs/github_jobs.js#L22

I would recommend you try that out and if you haven't already, I would ask that you also switch over your plugin to the Spice v2 format (ie. using a Handlebars template). Sorry if this creates any extra work -- we're hoping the new plugin format will be easier for developers to write and understand.

Please let me know if you have any more questions!

bradcater commented 11 years ago

Thanks, @moollaza. I didn't realize jQuery was available there. Using $ fixed everything. I was lucky enough to be using Handlebars already, just the wrong js library.

moollaza commented 11 years ago

@bradcater ah great! I'm glad jQuery solved your problems :)