Skip to content

Instantly share code, notes, and snippets.

@siannopollo
Created September 5, 2009 18:48
Show Gist options
  • Save siannopollo/181478 to your computer and use it in GitHub Desktop.
Save siannopollo/181478 to your computer and use it in GitHub Desktop.
// Mostly ripped off from here http://santrajan.blogspot.com/2008/10/what-john-resig-did-not-tell-you.html
// Brings a semblance of class hierarchy to jQuery, and allows object to do things, not have things done to them!
var JX = {
extend: function(bc, sc, o) {
var f = function() {};
f.prototype = sc.prototype;
bc.prototype = new f();
bc.prototype.constructor = bc;
bc.superclass = sc.prototype;
for (var m in o)
bc.prototype[m] = o[m];
}
};
JX.Component = function() {
JX.Component.superclass.init.apply(this, arguments);
};
JX.extend(JX.Component, jQuery, {});
JX.Class = function() {
JX.Class.superclass.constructor.apply(this, arguments);
};
JX.extend(JX.Class, JX.Component, {});
JX.createClass = function(name, superclass, definition) {
JX[name] = function() {
JX[name].superclass.constructor.apply(this, arguments);
// Allows you to find DOM elements with $() and ask that element for it's JX object
// $('#some_div')[0].containerObject
var objectName = name.substring(0,1).toLowerCase() + name.substring(1, name.length) + 'Object';
if (this[0] != undefined) this[0][objectName] = this;
// This removes 'this' from being the first parameter always passed to initialize
var args = Array.prototype.slice.call(arguments);
this.initialize.apply(this, args.splice(1, (args.length - 1)));
};
JX.extend(JX[name], superclass, definition);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment