Skip to content

Instantly share code, notes, and snippets.

@amiel
Created October 21, 2009 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/215384 to your computer and use it in GitHub Desktop.
Save amiel/215384 to your computer and use it in GitHub Desktop.
re-initialize
Base.initializers = {};
Base.init = function() {
if (arguments.length == 1) {
var to_init = arguments[0],
scope = $(to_init).not('.initialized').addClass('initialized');
$.each(Base.initializers[to_init], function(i, f) { f.apply(scope); });
} else {
var collection = (arguments.length == 0) ? Base.initializers : arguments;
$.each(collection, function(i, e) { if (typeof i === 'string') Base.init(i); else Base.init(e); });
}
};
Base.register_initializer = function(scope, lambda) {
if (!Base.initializers[scope]) Base.initializers[scope] = [];
Base.initializers[scope].push(lambda);
};
$(document).ready(function() {
Base.init();
});
Base.register_initializer('p', function() {
Base.debug('running p click init', this);
this.click(function() {
alert('bar');
});
});
Base.register_initializer('p', function() {
Base.debug('running p hover init', this);
this.hover(function() {
Base.debug('hover');
});
});
function some_ajax_callback(foo) {
$("body").append($('<p></p>').html(foo));
Base.init('p');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment