0

これがトリガーされないのはなぜですか?

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

これが次の場合:

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

イベントは次を使用して発生しています。

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

プラグイン内から。

これはブロック全体です:

/* 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));

#id がイベントをサブスクライブできるソリューションのような suplish/subscribe/observer のようなものを実装しようとしています

4

2 に答える 2

0

置い$(document).ready()てみてください。

$(document).ready(function() {
    $('#nav').bind('app:event', function (event, status) {
          console.log([event, status]);
    });
});
于 2009-10-14T13:49:15.057 に答える
0

非常に少ないコードで言うのは難しいです。$(this) をトリガーしようとしたときに $("#nav") を参照していますか?

于 2009-10-14T13:49:27.153 に答える