jquery data example
testing to make sure scope is stable in jquery data objects
by nilloc
HTML
<div id="tester" title="tester">testing...</div>
JavaScript
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function(){
var $this = $(this),
data = $this.data('tooltip'),
tooltip = $('<div />', {
text : $this.attr('title')
});
// If the plugin hasn't been initialized yet
if ( ! data ) {
/*
Do more setup stuff here
*/
$(this).data('tooltip', {
target : $this,
tooltip : tooltip
});
}
});
},
destroy : function( ) {
return this.each(function(){
var $this = $(this),
data = $this.data('tooltip');
// Namespacing FTW
$(window).unbind('.tooltip');
data.tooltip.remove();
$this.removeData('tooltip');
})
},
reposition : function( ) { /* ... */ },
show : function( ) { /* ... */ },
hide : function( ) { /* ... */ },
update : function( content ) { /* ... */ }
};
$.fn.tooltip = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery );
//var dataTester = {
// init:function( val ){
// alert(val + " works");
// }
//}
//$('#tester').data('dataTester', dataTester);
//$('#tester').data('dataTester').init( "data" );