Skip to content

Instantly share code, notes, and snippets.

@seyDoggy
Last active December 15, 2015 04:09
Show Gist options
  • Save seyDoggy/5199713 to your computer and use it in GitHub Desktop.
Save seyDoggy/5199713 to your computer and use it in GitHub Desktop.
stacks addon starter script
/**
* Script for myStack
*
* A super awesome stack of awesomeness
* http://some.url/mystack
*
* Licensed under the GPL 2
* Copyright 2013 Adam Merrifield
*/
/**
* stack
*
* public object
*
* The only reason we attach anything to this stack object is
* because Stacks return the `stack` variable when it puts all
* this code together. Knowing this allows us to fire functions
* without actually calling them ourselves.
*/
stack = {
/**
* postDom
*
* private method
*
* For ease of reading we put the code that we interact with
* (our interface) at the top. This is the code that gets
* fired on document ready so we put it in a short hand
* ready handler.
*
* Note that the functions we call here don't actually
* exist yet. That's ok because this part will wait for the
* DOM to be ready. At that point, those functions will be
* stored in memory.
*/
postDom : $(function () {
/**
* Method calls
*
* Now that the global methods are written (or cached)
* you can use them as needed.
*/
/*
* Store the global
*/
var myStack = stacks.myStack;
/*
* Set id
*/
var thisID = myStack.setId('%id%');
/*
* Set Option
*/
myStack.setOption = '%id=someOption%';
/*
* Call myTallest
*/
myStack.callTallest(thisID);
}),
/**
* preDom
*
* private method
*
* Since we are just caching these function for later use
* there is no need for the DOM to be ready at the time of
* declaring the following methods. This will speed things
* up a bit.
*
* Note that preDom is an anonymous, self invoking function.
* It does not to be called since it calls itself.
*/
preDom : (function() {
/*
* Test to see if the stacks.myStack global object
* has been defined already by another instance of
* the "myStack" stack. If it hasn't been defined
* then it will be now, and stored for the next
* instance.
*/
if (!stacks.myStack) {
/**
* stacks.myStack
*
* public object
*
* This global object is a method of the
* "stacks" global object and serves to hold the
* methods used across all instances of the
* "myStack" stack.
*
* Be sure not to pollute the "stacks" namespace!
* Make sure your namespaces are unique and not
* likely to be duplicated by other stack developers.
*/
stacks.myStack = {
/**
* Set Id
*
* public method
*
* This method sets the stack id.
*/
setId : function (id) {
var getId = id;
return getId;
},
/**
* Set Option
*
* public variable
*
* This variable holds a user selected option.
*/
setOption : null,
/**
* Set Element
*
* private static method
*
* This method sets an element object for each
* someElement found in the stack id.
*/
setElement : function (id) {
var getElement = $('#' + id + ' .someElement');
return getElement;
},
/**
* Call MyTallest
*
* public method
*
* This method fires the jQuery plugin included
* at the bottom of this script.
*
* It uses the instance variable this.setOption
* to test the users response to a stack option,
* then selects the DOM objects obtained by the
* static method this.setElement and finally
* fires the myTallest plugin.
*/
callMyTallest : function(id) {
if (this.setOption == true) {
this.setElement(id).myTallest();
}
}
};
} else {
/*
* If the test fails then the global "myStack"
* methods have been written and are properly
* cached. Your work here is done.
*
* This log is for illustration purposes only.
* This whole 'else' portion of the if-then-else
* statement can be removed unless you have
* something you'd like to do if the global
* methods are already written.
*/
console.log('stacks.myStack is already defined.');
}
})()
};
/**
* myTallest - Some Random jQuery plugin
*
* We test first to see if it's been written already with a
* simple if statement. If the statement is true then we cache
* the jQuery plugin. If it's false then it's already cached.
*
* Notice how we place our plugins at the very end of the code?
* It gets them out of the way to keep our code easier to read.
* Alternatively you can place them in assets and include them
* via ajax but your luck with that may vary.
*/
if (!jQuery.myTallest) {
(function($){
$.fn.myTallest = function(){
var tallest = 0,
thisTallest = 0;
$(this).each(function(){
thisTallest = $(this).height();
if (thisTallest > tallest) tallest = thisTallest;
}).height(tallest);
return $(this);
};
})(jQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment