public
Created

  • Download Gist
Class.Mutators.jQuery.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(function(){
 
Class.Mutators.jQuery = function(name){
var self = this;
jQuery.fn[name] = function(arg){
var args = Array.prototype.slice.call(arguments, 1);
 
if ($type(arg) == 'string'){
var instance = $(this).data(name);
if (instance) instance[arg](args); // ????
} else {
$(this).data(name, new self(this.selector, jQuery.extend(self.prototype.options, arg)));
}
};
};
})();
Person.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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
}
 
});
usage.js
JavaScript
1 2 3 4 5 6 7
// 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');

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.