Skip to content

Instantly share code, notes, and snippets.

@timoteoramos
Created January 18, 2011 14:48
Show Gist options
  • Save timoteoramos/784523 to your computer and use it in GitHub Desktop.
Save timoteoramos/784523 to your computer and use it in GitHub Desktop.
A basic draft of a jQuery-based project
"use strict";
(function() {
function BaseClass($) {
// element collection
var elements = [];
// if element isn't null, then set it as current element
var checkElement = function(el) {
if(el) {
window['Base'].CurrentElement = el;
}
return el;
}
this.CurrentElement = null;
this.Element = function(id) {
var el = null;
for(var i = 0; ++i < elements;) {
if(elements[i] && elements[i].id == id) {
// element found - set a temporary variable
el = checkElement(elements[i].obj);
// return it and finish this function
return el;
}
}
// element not found - using getElementById and appending it to list
var el = checkElement(document.getElementById(id));
elements.push({
id: id,
obj: el
});
// return the variable even though result is null
return el;
};
}
window['Base'] = new BaseClass(jQuery);
}) ();
jQuery(function($) {
// Example 1: WYSIWYG content and ews detail
if(Base.Element('content') || Base.Element('news-detail')) {
// this block is executed only if document has elements with IDs 'content' or 'news-detail'
// note that Base.CurrentElement can be any not-null option (it can be content or news detail)
$(Base.CurrentElement).find('a.lightbox').lightBox();
}
// Example 2: Contact form
if(Base.Element('contact')) {
$(Base.CurrentElement).find('> form').each(function() {
// i can write these blocks as a plugin-like style too :)
var $this = $(this);
$this.find('input').doSomethingNice();
});
}
});
@timoteoramos
Copy link
Author

This is a initial draft of my jQuery project skeleton. I use it to centralize all my javascript code into a single JS file, avoiding inline javascript in my HTML files. In next versions, I'll write suggestions for IE detection, IE specific versions (with and without modernizr) and other cool stuff.

@timoteoramos
Copy link
Author

Basic adjustment: append the new element to list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment