Binding events in a plugin
I'm trying to write a jQuery Websocket wrapper. I realise there are a couple out there, but I am using this as a means to learn how to write jQuery plugins.
I have nothing websocketty written yet, I'm having a more fundamental issue.
My code, thus far, is as follows
- (function($){
- var methods = {
- init:function(options)
- {
- console.log("Innited ");
- return this.each(function()
- {
-
}
- );
- },
- poke:function()
- {
- console.log("poked");
- }
- };
- $.websocket = function(method)
- {
- if ( methods[method] )
- {
- return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
- }
- else if ( typeof method === 'object' || ! method )
- {
- var self = methods.init.apply(this, arguments);
- this.bind('poke', methods.poke);
- return self;
- }
- else
- {
- $.error( 'Method ' + method + ' does not exist on jQuery.websocket' );
- }
- }
- })(jQuery);
In the html file I have included it in, I have
- $(
- function()
- {
- var test = $.websocket();
- console.log("Websocket inited?");
- $(test).trigger('poke');
- }
- );
But I'm not getting the event method being invoked.
$.websocket('poke'); does exactly what I'd anticipate it would do, but I'm having difficulty figuring out how to get it to do what I want.