Created
September 22, 2010 13:12
-
-
Save ehynds/591632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// our "Widget" object constructor | |
var Widget = function( options, element ){ | |
this.options = options; | |
this.element = element; | |
this._init(); | |
}; | |
Widget.prototype = { | |
// _init fires when your instance is first created | |
// (from the constructor above), and when you | |
// attempt to initialize the widget again (by the bridge) | |
// after it has already been initialized. | |
_init: function(){ | |
// init code | |
}, | |
option: function( key, value ){ | |
// get/change options AFTER initialization: | |
// you don't have to support all these cases, | |
// but here's how: | |
// signature: $('#foo').bar({ cool:false }); | |
if( $.isPlainObject( key ) ){ | |
this.options = $.extend(true, this.options, key); | |
// signature: $('#foo').option('cool'); - getter | |
} else if ( key && typeof value === "undefined" ){ | |
return this.options[ key ]; | |
// signature: $('#foo').bar('option', 'baz', false); | |
} else { | |
this.options[ key ] = value; | |
} | |
return this; // make sure to return the instance! | |
}, | |
publicFn: function(){ // notice no underscore | |
return "public method"; | |
}, | |
_privateFn: function(){ | |
return "private method"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment