Skip to content

Instantly share code, notes, and snippets.

@appden
Forked from ryanflorence/Class.Mutators.jQuery.js
Created April 23, 2010 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appden/376133 to your computer and use it in GitHub Desktop.
Save appden/376133 to your computer and use it in GitHub Desktop.
Class.Mutators.jQuery = function(name){
var self = this;
jQuery.fn[name] = function(arg){
var instance = this.data(name);
if ($type(arg) == 'string'){
var prop = instance[arg];
if ($type(prop) == 'function'){
var returns = prop.apply(instance, Array.slice(arguments, 1));
return (returns == instance) ? this : returns;
} else if (arguments.length == 1){
return prop;
}
instance[arg] = arguments[1];
} else {
if (instance) return instance;
this.data(name, new self(this.selector, arg));
}
return this;
};
};
var Person = new Class({
Implements: Options,
options: {
height: 'tall',
weight: 'fat'
},
jQuery: 'person', // must be after options definition
initialize: function(selector, options){
this.setOptions(options);
this.jqueryObject = jQuery(selector);
},
dance: function(whichDance){
// dance the whichDance
},
combust: function(){
// combust
}
});
// instantiate the class
var instance = new Person('#dude',{ height: 'short' });
jQuery('#dude').person({ height: 'short' });
// call methods
instance.dance('salsa');
jQuery('#dude').person('dance','salsa').person('otherMethod');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment