Last active
August 29, 2015 13:58
-
-
Save docluv/9958620 to your computer and use it in GitHub Desktop.
This is a Visual Studio code snippet to generate an extensible JavaScript module based on the 'jQuery Pattern'
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
<?xml version="1.0" encoding="utf-8"?> | |
<CodeSnippets | |
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Custom JavaScript Module</Title> | |
<Author>Chris Love</Author> | |
<Description>Creates the Base Code for a Stand Alone JavaScript Module</Description> | |
<Shortcut>module</Shortcut> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal> | |
<ID>modulename</ID> | |
<ToolTip>Module Name</ToolTip> | |
<Default>mymodule</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="JavaScript"> | |
<![CDATA[ | |
; | |
//Example JavaScript Module based on the jQuery pattern | |
//It is wrapped within an anymous enclosure | |
(function(window, undefined) { | |
//this is ultimately the object used to create the global variable | |
//it is set at the end of the module | |
//I use $modulename$ as an example name, you can do a replace all to name it | |
//what every suites your needs. | |
var $modulename$ = function(customSettings) { | |
//return the object created by the init method (defined later). | |
//notice we are calling the $modulename$ init method from the object's protype alias. | |
//The customSettings parameter is passed to the init method. Remember to pass | |
//any parameters along to the init method. | |
var that = new $modulename$.fn.init(customSettings); | |
//here I had a delima. I want to show how to merge a custom settings object | |
//but I don't wan to rely on jQuery, Underscore or something else for this example. | |
//so I decided to show you what it would look like with a jQuery dependancy. | |
//this.settings = utility.extend({}, this.settings, customSettings); | |
//for a lightweight library with an extend method see my dollarbill repository | |
//https://github.com/docluv/dollarbill | |
//and then just a simple little way to override the default settings. | |
//I do encourage merging the values though. | |
if(customSettings){ | |
that.settings = customSettings; | |
} | |
//you can set values in the object's init method, good for items with no definition | |
that.exampleVar = 0; | |
//This is actually where jQuery select the DOM element(s) you are looking for and encapsulates them. | |
return that; | |
}; | |
//Create an alias to the module's prototype | |
//create the object's members in the protype definition. | |
$modulename$.fn = $modulename$.prototype = { | |
//hmm what is this for? | |
//well combined with the following init definition we | |
constructor: $modulename$, | |
//gets everything started and returns a reference to the object. | |
//notice it was called from the $modulename$ function definition above. | |
init: function(customSettings) { | |
//return a reference to itself so you can chain things later! | |
return this; | |
}, | |
//I think this is just good practice ;) | |
version: "0.0.1", | |
//methods & other members go here | |
exampleVar: undefined, | |
//you can add as many members as you like, be they functions or just variables. | |
exampleFunc: function(){ | |
alert(this.exampleVar); | |
//to make it chainable you need to return a reference to your object's instance. | |
//you do not have to make your methods chainable, but it is a nice feature. | |
return this; | |
//remmeber the method should have a reference to this, which is a reference to the current $modulename$ instance. | |
//If you call other methods that do not belong to your instance then you should create a refernece to this. | |
//typically I do soemthing like this at the top of my methods: | |
// var that = this; | |
//then inside the other method I can refence the object's instance I want to call other methods. | |
//A common example would be a callback method from a jQuery object or using something like jQuery's each to iterrate over arrays. | |
}, | |
//returns the value of the settings.prop1 variable. | |
getProp1: function(){ | |
//just smart to check if the object still exist, otherwise you will get a nasty exception making your app break :( | |
if(!this.settings){ | |
return undefined; | |
} | |
return this.settings.prop1; | |
}, | |
//yes you can create chile objects | |
settings: { | |
prop1: "Sample Module" | |
} | |
}; | |
// Give the init function the $modulename$ prototype for later instantiation | |
$modulename$.fn.init.prototype = $modulename$.fn; | |
//create the global object used to create new instances of $modulename$ | |
return (window.$modulename$ = $modulename$); | |
})(window); | |
]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment