jQuery custom event
Why isn't this being triggered?
$('#nav').bind('app:event', function (event, status) {
console.log([event, status]);
});
when this is:
开发者_如何学Go$(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
Try to put $(document).ready()
around.
$(document).ready(function() {
$('#nav').bind('app:event', function (event, status) {
console.log([event, status]);
});
});
Difficult to say with so little of the code. Are you sure $(this) refers to $("#nav") when you try to trigger it?
精彩评论