TextMate Snippet #9

Merged
merged 2 commits into from Jul 26, 2011
+118 −0
Split
@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>content</key>
+ <string>(function(\$) {
+
+ // here it goes!
+ \$.fn.${1:pluginName} = function(method) {
+
+ // public methods
+ // to keep the \$.fn namespace uncluttered, collect all of the plugin's methods in an object literal and call
+ // them by passing the string name of the method to the plugin
+ //
+ // public methods can be called as
+ // element.$1('methodName', arg1, arg2, ... argn)
+ // where "element" is the element the plugin is attached to, "$1" is the name of your plugin and
+ // "methodName" is the name of a function available in the "methods" object below; arg1 ... argn are arguments
+ // to be passed to the method
+ //
+ // or, from inside the plugin:
+ // methods.methodName(arg1, arg2, ... argn)
+ // where "methodName" is the name of a function available in the "methods" object below
+ var methods = {
+
+ // this the constructor method that gets called when the object is created
+ init : function(options) {
+
+ // the plugin's final properties are the merged default and user-provided properties (if any)
+ // this has the advantage of not polluting the defaults, making them re-usable
+ this.$1.settings = \$.extend({}, this.$1.defaults, options);
+
+ // iterate through all the DOM elements we are attaching the plugin to
+ return this.each(function() {
+
+ var \$element = \$(this), // reference to the jQuery version of the current DOM element
+ element = this; // reference to the actual DOM element
+
+ // code goes here
+
+ });
+
+ },
+
+ // a public method. for demonstration purposes only - remove it!
+ foo_public_method: function() {
+
+ // code goes here
+
+ }
+
+ }
+
+ // private methods
+ // these methods can be called only from inside the plugin
+ //
+ // private methods can be called as
+ // helpers.methodName(arg1, arg2, ... argn)
+ // where "methodName" is the name of a function available in the "helpers" object below; arg1 ... argn are
+ // arguments to be passed to the method
+ var helpers = {
+
+ // a private method. for demonstration purposes only - remove it!
+ foo_private_method: function() {
+
+ // code goes here
+
+ }
+
+ }
+
+ // if a method as the given argument exists
+ if (methods[method]) {
+
+ // call the respective method
+ return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
+
+ // if an object is given as method OR nothing is given as argument
+ } else if (typeof method === 'object' || !method) {
+
+ // call the initialization method
+ return methods.init.apply(this, arguments);
+
+ // otherwise
+ } else {
+
+ // trigger an error
+ \$.error( 'Method "' + method + '" does not exist in $1 plugin!');
+
+ }
+
+ }
+
+ // plugin's default options
+ \$.fn.$1.defaults = {
+
+ foo: 'bar'
+
+ }
+
+ // this will hold the merged default and user-provided options
+ // you will have access to these options like:
+ // this.$1.settings.propertyName from inside the plugin or
+ // element.$1.settings.propertyName from outside the plugin, where "element" is the element the
+ // plugin is attached to;
+ \$.fn.$1.settings = {}
+
+})(jQuery);</string>
+ <key>name</key>
+ <string>plugin</string>
+ <key>scope</key>
+ <string>source.js</string>
+ <key>tabTrigger</key>
+ <string>plugin</string>
+ <key>uuid</key>
+ <string>0F6EFAA1-254D-47DF-914D-ACD9B78E08C0</string>
+</dict>
+</plist>