Last active
December 1, 2016 16:11
-
-
Save aubreypwd/97dd3ed1935986204a23 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. | |
var application = { | |
/** | |
* 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 application; // Return to jQuery for chaining. | |
}, | |
/** | |
* 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 application...' ); | |
console.log( application ); // Our stuff. | |
console.log( $target ); // jQuery stuff. | |
return application; // Return to jQuery for chaining. | |
}, | |
}; | |
application.__construct(); // Always run these things when the Object is created. | |
return application; // 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