-
-
Save gregrickaby/a20da5b4584dbccf2329 to your computer and use it in GitHub Desktop.
How to create a Js application as a jQuery plugin
This file contains hidden or 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
( function( $ ) { | |
// Create a jQuery Object. | |
$.fn.myObject = function() { | |
var target = this; // Store the target element in this variable (jQuery stuff). | |
// Our stuff. | |
this.object = { | |
/** | |
* Run the Construct every time we create a new instance. | |
* | |
* @return {Object} This object. | |
*/ | |
__construct: function() { | |
this.loaded(); // Show that this is loaded, remove when you're sure it's loading in your project. | |
return this; | |
}, | |
/** | |
* Loaded notice. | |
* | |
* This allows us to add this easily to a project and to give us feedback that it's loading properly. | |
* | |
* @return {Object} This object. | |
*/ | |
loaded: function() { | |
console.log( 'Loaded myObject...' ); | |
console.log( this ); // Our stuff. | |
console.log( target ); // jQuery stuff. | |
return this; | |
}, | |
}; | |
this.object.__construct(); // Always run these things when the Object is created. | |
return this.object; // return the object so we can access other properties. | |
}; | |
} ( jQuery ) ); | |
// Kick it off. | |
jQuery().myObject(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment