views:

69

answers:

2

Why isn't this being triggered?

$('#nav').bind('app:event', function (event, status) {
  console.log([event, status]);
});

when this is:

$(document).bind('app:event', function (event, status) {
  console.log([event, status]);
});

the event is being fired using:

$(this).trigger('app:event', [options]);

from within a plugin.

This is the entire block:

/* App.js */
$(function ($) {

  // UI Panels
  $('#panels').panels({
    controls: '#nav a'
  });

  $('#nav').bind('app:event', function (event, status) {
    console.log([event, status]);
  });

});

/**
 * Plugin dir
 */
(function($) {
  // Interface
  $.fn.panels = function (options) {
    var settings = {}, current;

    if (options) {
      $.extend(settings, options);
    }

    // Setup
    $('.panel', this).hide();
    $('.panel:first', this).slideDown('slow');

    return this.each(function () {
      var self = $(this);

      $(settings.controls).click(function () {
        // Get reference
        current = this.href.substring(this.href.indexOf('#') + 1);

        // Hide all
        $('.panel', self).hide();
        // Show current
        $('#'+ current)
          .publish({            
            'panelReady': current
          })
          .slideDown();

        return false;
      });
    });
  };

  // Publish event
  $.fn.publish = function (options) {    
    var settings = {
      origin: this      
    };

    if (options) {
      $.extend(settings, options);
    }

    return this.each(function () {
      $(this).trigger('app:event', [options]);
    });
  };

}(jQuery));

I'm trying to implement something like a suplish/subscribe/observer like solution where a #id can subscribe to events

A: 

Try to put $(document).ready() around.

$(document).ready(function() {
    $('#nav').bind('app:event', function (event, status) {
          console.log([event, status]);
    });
});
powtac
nope, didn't do it, it's already inside a $(function($) {}); but moving it to $(document).ready(); didn't change anything.
kristian nissen
A: 

Difficult to say with so little of the code. Are you sure $(this) refers to $("#nav") when you try to trigger it?

Marius
I added the entire block
kristian nissen